Singleton Implemenatatio链接器错误

时间:2011-09-08 12:17:19

标签: visual-c++ singleton

class Game
{
public:
    //! Returns the only instance of the game object
    static shared_ptr<Game> GetGameInstance();
    //! Function with continous loop for game play
    void GamePlay();
    //! The hidden constructor
    ~Game();

private:
    //! The hidden constructor
    Game();
    static shared_ptr<Game> _game;
};



//.cpp file

#include "Game.h"

shared_ptr<Game> Game::_game;

shared_ptr<Game> Game::GetGameInstance()
{
    if(_game == NULL)
    {
        _game.reset(new Game);
    }
    return _game;
}

void Game::GamePlay()
{
    shared_ptr<Graphics> myGameGraphics;

    while(!myGameGraphics->UserForcedExit())
    {
        myGameGraphics->drawMaze();
    }

}

Game::~Game()
{

}

我有上面的代码,但编译给我一个链接器错误:

Game.obj:错误LNK2019:未解析的外部符号“private:__thiscall Game :: Game(void)”(?? 0Game @@ AAE @XZ)在函数“public:static class boost :: shared_ptr __cdecl Game: :GetGameInstance(void)“(?GetGameInstance @ Game @@ SA?AV?$ shared_ptr @ VGame @@@ boost @@ XZ)

任何人都可以帮忙......

1 个答案:

答案 0 :(得分:0)

试试这个:

shared_ptr<Game> Game::GetGameInstance()
{
    if(_game = NULL)
        _game = shared_ptr<Game>(new Game);

    return _game;
}