zeb*_*und 0 c++ pointers compiler-errors global-variables
我的源文件似乎给我一个错误,我不明白为什么它是我得到这个错误,因为它似乎有点徒劳.我还想了解的是我能做些什么才能达到同样的效果.我现在将省略细节,但如图所示,我希望这是一个全局对象指针.
码
#include "functions.h"
App::Game* game = new Game;
void display_func() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
game->render_objects();
glFlush();
}
void init(int argc, char** argv) {
glutInit(&argc, argv);
game->init_window();
glutDisplayFunc(display_func);
game->init();
glutMainLoop();
}
Run Code Online (Sandbox Code Playgroud)
编译错误
functions.cpp:3:23: error: expected type-specifier before ‘Game’
functions.cpp:3:23: error: cannot convert ‘int*’ to ‘App::Game*’ in initialization
Run Code Online (Sandbox Code Playgroud)
我知道我无法在头文件中初始化它,所以我有什么选择?
你需要写
App::Game* game = new App::Game;
Run Code Online (Sandbox Code Playgroud)
(App::Game
代替Game
).
编辑添加:哦,我应该解释错误消息,为了其他人得到类似的消息由于不同的原因.一旦你有以下事实,他们大多是不言自明的:
int
或char*
或string
或std::string
; 在这种情况下,您需要的类型说明符是App::Game
.int
得到提到只是因为编译器,为了继续解析而争先恐后地提出类型说明符,选择int
作为默认值.(如果你写了int *game = new;
,你就不会得到第二条错误信息.)