C++错误:预期的unqualified-id

Seb*_*Seb 31 c++

我收到错误"错误:第6行的'{'token'之前的预期unqualified-id.

我不知道出了什么问题.

#include <iostream>

using namespace std;

class WordGame;
{
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();

}
Run Code Online (Sandbox Code Playgroud)

Ale*_*x Z 23

这里不应该有分号:

class WordGame;
Run Code Online (Sandbox Code Playgroud)

......但是在你的课程定义的最后应该有一个:

...
private:
    string theWord;
}; // <-- Semicolon should be at the end of your class definition
Run Code Online (Sandbox Code Playgroud)


kee*_*r12 8

作为旁注,请考虑将setWord()中的字符串作为const引用传递,以避免过多的复制.另外,在displayWord中,考虑使这个const函数遵循const-correctness.

void setWord(const std::string& word) {
  theWord = word;
}
Run Code Online (Sandbox Code Playgroud)


Bet*_*eta 7

之后摆脱分号WordGame.

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