Visual C++:编译器错误 C4430
Game.h的代码:
#ifndef GAME_H
#define GAME_H
class Game
{
public:
const static string QUIT_GAME; // line 8
virtual void playGame() = 0;
};
#endif
错误:
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
我做错了什么?
The code of Game.h:
#ifndef GAME_H
#define GAME_H
class Game
{
public:
const static string QUIT_GAME; // line 8
virtual void playGame() = 0;
};
#endif
The error:
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
What am I doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您需要做两件事:
#include
const static std::string QUIT_GAME
(添加std::
)You need to do two things:
#include <string>
const static std::string QUIT_GAME
(addingstd::
)以下是解决问题所需的内容:
1.包含字符串头文件:
#include
2.前缀
string
及其命名空间:const static std::string QUIT_GAME;
或插入
using
语句:3.为变量分配空间
由于您在类中将其声明为
static
,因此它必须在代码中的某个位置定义:const std::string Game::QUIT_GAME;
4.使用值初始化变量
由于您使用
const
声明了该字符串,因此您需要将其初始化为一个值(否则它将保持为常量空字符串)。:const std::string Game::QUIT_GAME = "你想退出吗?\n";
Here is what you need to fix your issues:
1. Include the string header file:
#include <string>
2. Prefix
string
with its namespace:const static std::string QUIT_GAME;
or insert a
using
statement:3. Allocate space for the variable
Since you declared it as
static
within the class, it must be defined somewhere in the code:const std::string Game::QUIT_GAME;
4. Initialize the variable with a value
Since you declared the string with
const
, you will need to initialize it to a value (or it will remain a constant empty string).:const std::string Game::QUIT_GAME = "Do you want to quit?\n";
缺少
#include
并且它是
std::string
Missing the
#include<string>
and it's
std::string
尝试在顶部添加:
try adding at the top:
就我而言,我在
#define
上有拼写错误,我引用了 这篇文章创建dll项目,正确的是头文件中的wrting followiung:但我有拼写错误丢失某些字符在
#else #define
中有所不同:将两者修复为相同的作品后。
In my case, I have typo on
#define
, I reference to this article to create dll project, and correct is wrting followiung in header file:but I have typo lose some character and different in
#else #define
:after fixing both to the same works.