[英]How to fix C++ error: expected unqualified-id
I'm getting this error on line 6:我在第 6 行收到此错误:
error: expected unqualified-id before '{' token
I can't tell what's wrong.我说不出怎么回事。
#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();
}
There should be no semicolon here:这里不应该有分号:
class WordGame;
...but there should be one at the end of your class definition: ...但是在类定义的末尾应该有一个:
...
private:
string theWord;
}; // <-- Semicolon should be at the end of your class definition
As a side note, consider passing strings in setWord() as const references to avoid excess copying.作为旁注,请考虑将 setWord() 中的字符串作为常量引用传递以避免过度复制。 Also, in displayWord, consider making this a const function to follow const-correctness.
此外,在 displayWord 中,考虑将其设为 const 函数以遵循常量正确性。
void setWord(const std::string& word) {
theWord = word;
}
Get rid of the semicolon after WordGame
.去掉
WordGame
后的分号。
You really should have discovered this problem when the class was a lot smaller.你真的应该在班级小得多的时候发现这个问题。 When you're writing code, you should be compiling about every time you add half a dozen lines.
在编写代码时,每次添加六行代码时都应该进行编译。
Semicolon should be at the end of the class definition rather than after the name:分号应该在类定义的末尾而不是名称之后:
class WordGame
{
};
For what it's worth, I had the same problem but it wasn't because of an extra semicolon, it was because I'd forgotten a semicolon on the previous statement.无论如何,我遇到了同样的问题,但这不是因为额外的分号,而是因为我忘记了前一条语句中的分号。
My situation was something like我的情况是这样的
mynamespace::MyObject otherObject
for (const auto& element: otherObject.myVector) {
// execute arbitrary code on element
//...
//...
}
From this code, my compiler kept telling me:从这段代码中,我的编译器不断告诉我:
error: expected unqualified-id before for (const auto& element: otherObject.myVector) { etc...
which I'd taken to mean I'd writtten the for loop wrong. error: expected unqualified-id before for (const auto& element: otherObject.myVector) { etc...
我认为我写错了 for 循环。 Nope!不! I'd simply forgotten a
;
我只是忘记了一个
;
after declaring otherObject
.在声明
otherObject
。
For anyone with this situation: I saw this error when I accidentally used my_first_scope::my_second_scope::true
in place of simply true
, like this:对于遇到这种情况的任何人:当我不小心使用
my_first_scope::my_second_scope::true
代替简单的true
,我看到了这个错误,如下所示:
bool my_var = my_first_scope::my_second_scope::true;
instead of:代替:
bool my_var = true;
This is because I had a macro which caused MY_MACRO(true)
to expand into my_first_scope::my_second_scope::true
, by mistake, and I was actually calling bool my_var = MY_MACRO(true);
这是因为我有一个宏导致
MY_MACRO(true)
地扩展为my_first_scope::my_second_scope::true
,而我实际上是在调用bool my_var = MY_MACRO(true);
. .
Here's a quick demo of this type of scoping error:这是此类范围错误的快速演示:
Program (you can run it online here: https://onlinegdb.com/BkhFBoqUw ):程序(您可以在此处在线运行: 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 World\n");
bool my_var = my_first_scope::my_second_scope::true;
std::cout << my_var << std::endl;
return 0;
}
Output (build error):输出(构建错误):
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; ^~~~
Notice the error: error: expected unqualified-id before 'true'
, and where the arrow under the error is pointing.请注意错误:
error: expected unqualified-id before 'true'
,以及错误下方的箭头所指的位置。 Apparently the "unqualified-id" in my case is the double colon ( ::
) scope operator I have just before true
.显然,在我的例子中, “unqualified-id”是我在
true
之前的双冒号 ( ::
范围运算符。
When I add in the macro and use it (run this new code here: https://onlinegdb.com/H1eevs58D ):当我添加宏并使用它时(在此处运行此新代码: https : //onlinegdb.com/H1eevs58D ):
#define MY_MACRO(input) my_first_scope::my_second_scope::input
...
bool my_var = MY_MACRO(true);
I get this new error instead:我收到了这个新错误:
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 ^~~~~
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.