LNK2001 Singleton上未解析的外部符号

时间:2015-03-24 18:54:06

标签: c++ linker singleton game-engine

我正在使用SFML和Box2D创建一个类似peggle的游戏,但是我的游戏单身人士遇到了这个错误。

错误1错误LNK2001:未解析的外部符号"私人:静态类游戏*游戏:: s_pInstance" (?s_pInstance @ Game @@ 0PAV1 @ A)C:\ Users \ UserName \ Desktop \ PeggleClone \ Maze \ Maze \ Game.obj

Main.cpp的

#include "Game.h"
#define SCALE 30.0f
#define WINDOW_WIDTH 800
#define WINDOW_HEIGHT 600   

int _tmain(int argc, _TCHAR* argv[])
{
    Game* game = Game::GetInstance();

    game->Initialize(WINDOW_WIDTH, WINDOW_HEIGHT);

    //Stuff for timesteps
    float timenow = (float)GetTickCount();
    float timethen = (float)GetTickCount();
    float timeelapsed = 0.0f;


    while (true)
    {
        timeelapsed = (float)(timenow - timethen) / 1000.0f;

        if(game->Update(timeelapsed) < 0)
            break;
        game->Render();


        timethen = timenow;
        timenow = (float)GetTickCount();
    }

    game->Terminate();
    game->DeleteInstance();
    return 0;
}

Game.h

#pragma once


class BaseGameState;
class Game
{
public:
    void Initialize();


    /**********************************************************/
    // Singleton Accessors:
    static Game*    GetInstance     ( void );
    static void     DeleteInstance  ( void );


    /**********************************************************/
    // Setup, Play, Cleanup:
    bool Initialize ( unsigned int width, unsigned int height );
    int  Update     ( float elapsedTime );
    void Render     ( void );
    void Terminate  ( void );

    unsigned int GetWidth() {return m_uWidth;}
    unsigned int GetHeight() {return m_uHeight; }
    sf::RenderWindow* GetWindow() {return gameWindow;}


private:

    /**********************************************************/
    // Singleton Object:
    static Game*    s_pInstance;

    Game(void) = default;
    ~Game(void) = default;
    Game(const Game&) = delete; // copy constructor
    Game& operator= (const Game&) = delete; // assignment operator


    /**********************************************************/
    // Game State Machine:
    //  - can ONLY be called by the state's Input, Update, or Render methods!!!
    void PushState(BaseGameState* pNewState);
    void PopState(BaseGameState* pNewState);
    void ClearState(BaseGameState* pNewState);
    vector<BaseGameState*> m_vStates;




    // Window stuff
    unsigned int m_uWidth, m_uHeight;

    sf::RenderWindow* gameWindow;
};

Game.cpp

#include "stdafx.h"
#include "Game.h"
#include "BaseGameState.h"
#include "MainMenuState.h"



bool Game::Initialize(unsigned int width, unsigned int height)
{
    // Set up render window
    gameWindow = new sf::RenderWindow(sf::VideoMode(width, height), "Peggle Ripoff");

    // Clear state machine
    m_vStates.clear();

    //Start on main menu
    PushState(MainMenuState::GetInstance());

    return true;
}

Game* Game::GetInstance( void )
{
    if( s_pInstance == nullptr )
        s_pInstance = new Game;

    return s_pInstance;
}

void Game::DeleteInstance( void )
{
    delete s_pInstance;
    s_pInstance = nullptr;
}

1 个答案:

答案 0 :(得分:1)

修正了它,我需要通过在Game.cpp顶部添加这一行来初始化游戏实例

Game* Game::s_pInstance = nullptr;