I'm compiling a C++ program which and I get the error "two or more data types in declaration" at the line below. Here is the code:
#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;
}
I get the error:
error: two or more data types in declaration of 'x'
There's more coding but it's too long and I think it's not related to the error.
How do I fix it. Thanks!
for(:)
is range-based for loop and is a c++ 11
feature of the language. So use of bellow insted of for(:)
statement :
for each (char x in infix)
As far as I know, for(:)
is compatible with g++
compiler and it may be incompatible with anyother compilers.
Then remove x
declaration from code :
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;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.