[英]Error: two or more data types in declaration of 'x'
我正在编译一个 C++ 程序,我在下面的行中收到错误“声明中有两种或多种数据类型”。 这是代码:
#include <iostream>
#include <string>
#include <sstream>
#include <stdlib.h>
List SplitInflix(const string infix)
{
List tokenlist;
string word= "";
char x;
for (char x : infix)
{
switch (x)
{
case '(':
if (word != "")
{
Token* token = new Token(word);
tokenlist.append(token);
word = "";
Token * token1 = new Token(LEFT);
tokenlist.append(token1);
}
else
{
Token * token = new Token(LEFT);
tokenlist.append(token);
}
break;
...
}
return tokenlist;
}
我收到错误:
错误:'x' 的声明中有两种或多种数据类型
还有更多的编码,但太长了,我认为这与错误无关。
我如何解决它。 谢谢!
for(:)
是基于范围的for 循环,是该语言的c++ 11
特性。 所以使用下面的for(:)
语句:
for each (char x in infix)
据我所知, for(:)
与g++
编译器兼容,并且可能与任何其他编译器不兼容。
然后从代码中删除x
声明:
List SplitInflix(const string infix)
{
List tokenlist;
string word= "";
// char x; Theres no need to this line
for each (char x in infix)
{
switch (x)
{
case '(':
if (word != "")
{
Token* token = new Token(word);
tokenlist.append(token);
word = "";
Token * token1 = new Token(LEFT);
tokenlist.append(token1);
}
else
{
Token * token = new Token(LEFT);
tokenlist.append(token);
}
break;
...
}
return tokenlist;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.