Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/127.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++;和SDL2-创建仅常量标题:对constants::window的未定义引用 因此,我用C++和SDL 2开始了一个自由时间游戏项目,并陷入了一个令人困惑的问题。_C++_Sdl 2 - Fatal编程技术网

C++;和SDL2-创建仅常量标题:对constants::window的未定义引用 因此,我用C++和SDL 2开始了一个自由时间游戏项目,并陷入了一个令人困惑的问题。

C++;和SDL2-创建仅常量标题:对constants::window的未定义引用 因此,我用C++和SDL 2开始了一个自由时间游戏项目,并陷入了一个令人困惑的问题。,c++,sdl-2,C++,Sdl 2,我有一个头文件,打算在其中存储全局使用的数据 class Constants { public: static SDL_Window* window; const static int w = 640; const static int h = 480; }; 现在,当我在别处引用窗口时,会出现一个“未定义引用”错误。我曾尝试在主函数之前添加空定义,但仍然不起作用 所以问题出现在这里: SDLManager::SDLManager() { // ......

我有一个头文件,打算在其中存储全局使用的数据

class Constants {
public:
    static SDL_Window* window;

    const static int w = 640;
    const static int h = 480;
};
现在,当我在别处引用窗口时,会出现一个“未定义引用”错误。我曾尝试在主函数之前添加空定义,但仍然不起作用

所以问题出现在这里:

SDLManager::SDLManager() {

    // ......

    // THIS BREAKS
    Constants::window = SDL_CreateWindow("Caption", 
        SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 
        Constants::w, Constants::h, SDL_WINDOW_SHOWN);
}

出了什么问题,我该怎么办?我以前已经成功地使用SDL 1进行了编码。

至少,您需要在标头中向前声明SDL\u窗口类


此外,如果以上是链接器错误,您需要让编译器知道链接到哪个库以及在哪里找到它。

我在自己的游戏开发过程中也遇到过类似的问题(尽管我使用了Allegro)

您需要做的是定义有问题的变量/类。你只申报了。您现在需要定义它

// constants.h
class Constants {
    public:
    static SDL_Window* window;

    const static int w = 640;
    const static int h = 480;
};

// constants.cpp
#include "constants.h"

Constants::SDL_Window *window;

如果我的语法有点不正确,我希望得到纠正。

您可能需要澄清是否使用C++11,只是想一想。非常感谢,这很有效!定义为SDL_Window*Constants::Window。