Search code examples
c++lnk2001

Why doesn't this Inline header compile?


I come from a .NET and Java background, and I'm trying to create a simple scene manager for my game. It's an inline header file, and I'm getting errors compiling.

#pragma once
#include "Scene.h"
class SceneManager
{
private:
    static Scene currentScene;

public:    

    SceneManager()
    {
    }

    static void SetScene(Scene scene)
    {
        currentScene = scene;
    }

    static Scene GetScene()
    {
        return currentScene;
    }
};

EDIT: I am getting this error:

Error   1   error LNK2001: unresolved external symbol "private: static class Scene SceneManager::currentScene" (?currentScene@SceneManager@@0VScene@@A) c:\Users\Justin\documents\visual studio 2013\Projects\Noeron\Noeron\main.obj    Noeron

Solution

  • Static member variables need to be not only declared, but defined. The declaration belongs in the header file, and the definition should go into a source file - you only want one of them in the entire program.

    Scene SceneManager::currentScene;