c++ - 如何修复 C++ 错误 : expected unqualified-id

标签 c++

我在第 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

关于c++ - 如何修复 C++ 错误 : expected unqualified-id,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10135244/

相关文章:

c++ - 如何使用 date.h 在 C++ 中获取当前星期几?

c++ - 使用 MFC 中的异步过程调用中断接受 winsock 调用

c++ - 将 MFC 形式 'text' 作为 char 类型或常规字符串类型处理

c++ - 将数据从调用者线程传递到另一个 boost::thread 中的方法

c++ - 将函数和成员函数作为参数传递给另一个函数

c++ - 在生产者继续之前等待所有异步消费者完成

c++ - Visual Studio 08鼠标悬停时的函数信息显示

c++ - 嵌套 boost::lambda::bind-s 问题

c++ - 匹配继承的成员函数的类型

c++ - 查找数组中缺失的数字