Nic*_*ner 2 c++ syntax-error visual-c++
Game.h的代码:
#ifndef GAME_H
#define GAME_H
class Game
{
public:
const static string QUIT_GAME; // line 8
virtual void playGame() = 0;
};
#endif
Run Code Online (Sandbox Code Playgroud)
错误:
game.h(8): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
game.h(8): error C2146: syntax error : missing ';' before identifier 'QUIT_GAME'
game.h(8): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
Run Code Online (Sandbox Code Playgroud)
我究竟做错了什么?
你需要做两件事:
#include <string>
const static std::string QUIT_GAME
(添加std::
)以下是解决问题所需的内容:
1.包含字符串头文件:
#include <string>
2.前缀string
及其命名空间:
const static std::string QUIT_GAME;
或插入一份using
声明:
#include <string>
using std::string;
Run Code Online (Sandbox Code Playgroud)
3.为变量分配空间
因为您static
在类中声明它,所以必须在代码中的某处定义它:
const std::string Game::QUIT_GAME;
4.用值初始化变量
因为你声明了字符串const
,你需要将它初始化为一个值(或者它将保持一个常量空字符串):
const std::string Game::QUIT_GAME = "Do you want to quit?\n";
归档时间: |
|
查看次数: |
10806 次 |
最近记录: |