0

I am creating a peggle-like game with SFML and Box2D but am getting this error with my game singleton.

Error 1 error LNK2001: unresolved external symbol "private: static class Game * Game::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;
}
user3334986
  • 149
  • 1
  • 12
  • Additionally I would rather recommend using [Scott Meyer's Singleton idiom](http://stackoverflow.com/questions/23690416/c-template-singleton-static-pointer-initialization-in-header-file/23690469#23690469), if you are sure to need one. – πάντα ῥεῖ Mar 24 '15 at 18:59

1 Answers1

2

Fixed it, I needed to initialize the game instance by adding this line at the top of Game.cpp

Game* Game::s_pInstance = nullptr;
user3334986
  • 149
  • 1
  • 12