我收到错误"错误:第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)
作为旁注,请考虑将setWord()中的字符串作为const引用传递,以避免过多的复制.另外,在displayWord中,考虑使这个const函数遵循const-correctness.
void setWord(const std::string& word) {
theWord = word;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
191787 次 |
| 最近记录: |