未定义的静态变量引用

未定义的静态变量引用

本文介绍了c ++未定义的静态变量引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道为什么这个代码不工作。所有的源文件编译,但是当我尝试链接他们编译器对我说一个未定义的引用错误。这里是代码:

I have no idea why this code isn't working. All the source files compile but when I try to link them the compiler yells at me with an undefined reference error. Here's the code:

main.cpp:

#include "SDL/SDL.h"
#include "Initilize.cpp"

int main(int argc, char* args[])
{
    //Keeps the program looping
    bool quit = false;
    SDL_Event exit;
    //Initilizes, checks for errors
    if(Initilize::Start() == -1)
    {
        SDL_Quit();
    }
    //main program loop
    while(quit == false)
    {
        //checks for events
        while(SDL_PollEvent(&exit))
        {
            //checks for type of event;
            switch(exit.type)
            {
                case SDL_QUIT:
                quit = true;
                break;
            }
        }
    }
    return 0;
}

Initilize.h:

Initilize.h:

#ifndef INITILIZE_H
#define INITILIZE_H
#include "SDL/SDL.h"

/* Declares surface screen, its attributes, and Start(); */
class Initilize {
protected:
    static SDL_Surface* screen;
private:
    static int SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP;
public:
    static int Start();
};

#endif

Initilize.cpp:

Initilize.cpp:

#include "Initilize.h"
#include "SDL/SDL.h"

/* Initilizes SDL subsystems, sets the screen, and checks for errors */
int Initilize::Start()
{
    //screen attributes
    SCREEN_WIDTH = 640;
    SCREEN_HEIGHT = 480;
    //Bits per pixel
    SCREEN_BPP = 32;
    //Inits all subsystems, if there's an error, return 1
    if(SDL_Init(SDL_INIT_EVERYTHING) == -1) {
            return 1;
    }
    //sets screen
    screen = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_SWSURFACE);
    //Returns 1 if there was in error with setting the screen
    if(screen == NULL) {
            return 1;
    }
    SDL_WM_SetCaption("Game", NULL);
    return 0;
}

对不起,如果代码格式奇怪,插入四个空格放入代码

Sorry if the code was formatted weirdly, inserting four spaces to put in a code block messed things up a little bit.

推荐答案

将以下内容添加到您的cpp文件中:

Add the following to your cpp file:

SDL_Surface* Initilize::screen = 0; // or nullptr
int Initilize::SCREEN_WIDTH = 640;
int Initilize::SCREEN_HEIGHT = 480;
int Initilize::SCREEN_BPP = 32;

此外,如果这些值永远不变,那么最好让它们 const 。您需要将上述添加到您的cpp文件的原因是因为静态成员变量需要在类外部定义。 static SDL_Surface * screen; 等在你的类中是声明,而不是定义。 static 成员被认为是特殊的,并且非常类似于全局变量。

Also, if these value never change, it would be good to make them const. The reason you need to add the above to your cpp file is because static member variables need to be defined outside of the class. static SDL_Surface* screen;, etc. inside your class is only a declaration, and not a definition. static members are considered special and is very similar to a global variable.

这是因为静态成员在您的类的所有实例之间共享。这意味着它们只能被定义一次,并允许类中的定义会导致出现多个定义,所以C ++标准强制你在类之外定义它(也意味着你应该将在cpp文件中定义)。

The reason for this is because static members are shared between all instances of your class. This means they can only be defined once and allowing the definition inside the class would cause multiple definitions to occur, so the C++ standard forces you to define it outside of your class (and also implies you should put the definition in a cpp file).

这篇关于c ++未定义的静态变量引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-01 17:13