如何修复C++错误:预期的非限定 ID

How to fix C++ error: expected unqualified-id

本文关键字:ID 何修复 C++ 错误      更新时间:2023-10-16

我在第 6 行收到此错误:

error: expected unqualified-id before '{' token

我说不出出了什么问题。

#include <iostream>
using namespace std;
class WordGame;
{               // <== error is here on line 6
public:
    void setWord( string word )
    {
        theWord = word;
    }
    string getWord()
    {
        return theWord;
    }
    void displayWord()
    {
        cout << "Your word is " << getWord() << endl;
    }
private:
    string theWord;
}

int main()
{
    string aWord;
    WordGame theGame;
    cin >> aWord;
    theGame.setWord(aWord);
    theGame.displaymessage();
}

这里不应该有分号:

class WordGame;

。但是在类定义的末尾应该有一个:

...
private:
    string theWord;
}; // <-- Semicolon should be at the end of your class definition

作为旁注,请考虑将 setWord() 中的字符串作为常量引用传递,以避免过度复制。 此外,在displayWord中,考虑将其设置为常量函数以遵循常量正确性。

void setWord(const std::string& word) {
  theWord = word;
}

去掉WordGame后的分号 .

当班级小得多时,你真的应该发现这个问题。当你在编写代码时,你应该在每次添加六行时进行编译。

号应该在类定义的末尾,而不是在名称之后:

class WordGame
{
};

对于它的价值,我遇到了同样的问题,但这不是因为额外的分号,而是因为我忘记了上一个语句中的分号。

我的情况是这样的

mynamespace::MyObject otherObject
for (const auto& element: otherObject.myVector) {
  // execute arbitrary code on element
  //...
  //...
} 

从这段代码中,我的编译器不断告诉我:

error: expected unqualified-id before for (const auto& element: otherObject.myVector) { etc... 我认为这意味着我写错了 for 循环。不!我只是在宣布otherObject后忘记了;.

对于任何遇到这种情况的人: 当我不小心使用 my_first_scope::my_second_scope::true 代替简单的 true 时,我看到了此错误,如下所示:

bool my_var = my_first_scope::my_second_scope::true;

而不是:

bool my_var = true;

这是因为我有一个宏,导致MY_MACRO(true)错误地扩展到my_first_scope::my_second_scope::true,我实际上是在调用bool my_var = MY_MACRO(true);

下面是此类范围错误的快速演示:

程序(您可以在此处在线运行:https://onlinegdb.com/BkhFBoqUw):

#include <iostream>
#include <cstdio>
namespace my_first_scope 
{
namespace my_second_scope
{
} // namespace my_second_scope
} // namespace my_first_scope
int main()
{
    printf("Hello Worldn");
    
    bool my_var = my_first_scope::my_second_scope::true;
    
    std::cout << my_var << std::endl;
    return 0;
}

输出(构建错误):

main.cpp: In function ‘int main()’:
main.cpp:27:52: error: expected unqualified-id before ‘true’
     bool my_var = my_first_scope::my_second_scope::true;
                                                    ^~~~

请注意错误:error: expected unqualified-id before ‘true’ ,以及错误下方的箭头指向的位置。显然,在我的情况下,"非限定 id"是我在true之前使用的双冒号 (::) 作用域运算符。

当我添加宏并使用它时(在此处运行此新代码:https://onlinegdb.com/H1eevs58D):

#define MY_MACRO(input) my_first_scope::my_second_scope::input
...
bool my_var = MY_MACRO(true);

我收到这个新错误:

main.cpp: In function ‘int main()’:
main.cpp:29:28: error: expected unqualified-id before ‘true’
     bool my_var = MY_MACRO(true);
                            ^
main.cpp:16:58: note: in definition of macro ‘MY_MACRO’
 #define MY_MACRO(input) my_first_scope::my_second_scope::input
                                                          ^~~~~

我收到此错误是因为我没有声明变量并进一步使用它.这是我的代码,为什么我得到它。

这是因为我没有为>向量的大小声明变量。只需更换

int n=arr.size();

在这里替换,

int sumSubarrayMins(vector<int>& arr) {
        
        
        int = arr.size();
        long long sum;
        long long ans =0;
        for(long i =0;i<n;i++){
            
             sum =0;
            long mini=INT_MAX;
            for(long long j =i;j<n;j++){
                mini=min(mini,arr[j]);
                sum+=mini;
            }
            ans+=sum;
           
        }
        return ans;
    }

你忘记了这个空的setWord(字符串字)