Visual C++:编译器错误 C4430

发布于 2024-09-03 10:26:42 字数 550 浏览 6 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

尽揽少女心 2024-09-10 10:26:42

您需要做两件事:

  • #include
  • 将类型更改为 const static std::string QUIT_GAME (添加 std::

You need to do two things:

  • #include <string>
  • Change the type to const static std::string QUIT_GAME (adding std::)
姜生凉生 2024-09-10 10:26:42

以下是解决问题所需的内容:

1.包含字符串头文件:
#include

2.前缀 string 及其命名空间:
const static std::string QUIT_GAME;

或插入 using 语句:

#include <string>
using std::string;

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:

#include <string>
using std::string;

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";

你是年少的欢喜 2024-09-10 10:26:42
#include <string>
...
const static std::string QUIT_GAME;
#include <string>
...
const static std::string QUIT_GAME;
能否归途做我良人 2024-09-10 10:26:42

缺少 #include

并且它是 std::string

Missing the #include<string>

and it's std::string

帥小哥 2024-09-10 10:26:42

尝试在顶部添加:

#include <string>
using std::string;

try adding at the top:

#include <string>
using std::string;
窗影残 2024-09-10 10:26:42

就我而言,我在 #define 上有拼写错误,我引用了 这篇文章创建dll项目,正确的是头文件中的wrting followiung:

#ifdef MATHLIBRARY_EXPORTS
#define MATHLIBRARY_API __declspec(dllexport)
#else
#define MATHLIBRARY_API __declspec(dllimport)
#endif

但我有拼写错误丢失某些字符在 #else #define 中有所不同:

#define MATHLIBRARY_API __declspec(dllexport)
#else
#define MATHLIARY_API __declspec(dllimport)

将两者修复为相同的作品后。

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:

#ifdef MATHLIBRARY_EXPORTS
#define MATHLIBRARY_API __declspec(dllexport)
#else
#define MATHLIBRARY_API __declspec(dllimport)
#endif

but I have typo lose some character and different in #else #define:

#define MATHLIBRARY_API __declspec(dllexport)
#else
#define MATHLIARY_API __declspec(dllimport)

after fixing both to the same works.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文