I’m trying to implement the singleton design pattern i found in a book, i’m aware that i might not need to use the singleton here but that aside i’ve created a GamePropertiesManager and i’m getting the following compilation (or rather, linker?) error: Main.cpp|| undefined reference to GamePropertiesManager::GetInstance
I’m trying to use my singleton in my main function like so..
#include "GamePropertiesManager.hpp"
..
int main()
{
GamePropertiesManager::Create();
GamePropertiesManager::GetInstance()->test();
...
}
My GamePropertiesManager is defined like this in a header file:
class GamePropertiesManager
{
public:
static GamePropertiesManager* GetInstance();
static void Create();
static void Destroy();
void test();
protected:
GamePropertiesManager();
static GamePropertiesManager* _instance;
enum GameMode{ PLAYERVSPLAYER, PLAYERVSCOM };
GameMode _gameMode;
std::string _player1Name, _player2Name;
int _player1Score, _player2Score;
std::string _matchSurvivor;
int _gameSpeed;
};
And declared in a separate cpp file:
#include "GamePropertiesManager.hpp"
//testic
#include <iostream>
GamePropertiesManager* GamePropertiesManager::_instance = NULL;
void GamePropertiesManager::Create()
{
if (!_instance) //instance not yet created
_instance = new GamePropertiesManager();
}
void GamePropertiesManager::Destroy()
{
delete _instance;
_instance = 0;
}
GamePropertiesManager::GamePropertiesManager() :
_gameMode(PLAYERVSCOM), _player1Name("Player 1"), _player2Name("Player 2"),
_player1Score(0), _player2Score(0), _matchSurvivor("NONE"), _gameSpeed(1)
{
}
void GamePropertiesManager::test()
{
std::cout << "test success!" << std::endl;
}
If someone could look over it an explain what i’m doing wrong i’d appreciate it very much!
You’ve declared a function
static GamePropertiesManager* GetInstance();but not defined it. Actually, yourGamePropertiesManager::Create()is doing most of the work. If I were you, I’d get rid of it and replace it with: