簡體   English   中英

錯誤:'x' 的聲明中有兩個或多個數據類型

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM