单例实现链接器错误

发布于 2024-12-03 16:52:59 字数 1066 浏览 1 评论 0原文

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 : error LNK2019: unresolved external symbol "private: __thiscall Game::Game(void)" (??0Game@@AAE@XZ) 在函数中引用“公共:静态类 boost::shared_ptr __cdecl Game::GetGameInstance(void)” (?GetGameInstance@Game@@SA?AV?$shared_ptr@VGame@@@boost@@XZ)

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

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()
{

}

I have the code above,but the compilation gives me a linker error:

Game.obj : error LNK2019: unresolved external symbol "private: __thiscall Game::Game(void)" (??0Game@@AAE@XZ) referenced in function "public: static class boost::shared_ptr __cdecl Game::GetGameInstance(void)" (?GetGameInstance@Game@@SA?AV?$shared_ptr@VGame@@@boost@@XZ)

Can anyone please help...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

通知家属抬走 2024-12-10 16:52:59

试试这个:

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

    return _game;
}

try this:

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

    return _game;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文