我試圖實現Singleton設計模式我在一本書中,我知道,我可能不需要在這裏使用Singleton但拋開我創建了一個GamePropertiesManager和我得到以下編譯(或者更確切地說,連接器?)錯誤:Main.cpp|| undefined reference to GamePropertiesManager::GetInstance
辛格爾頓執行情況和不確定的參考::的GetInstance
我試圖用我單身,我喜歡這樣的主要功能..
#include "GamePropertiesManager.hpp"
..
int main()
{
GamePropertiesManager::Create();
GamePropertiesManager::GetInstance()->test();
...
}
我GamePropertiesManager定義像這樣的一個頭文件:
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;
};
,並在單獨的cpp文件中聲明:
#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;
}
如果有人可以查看它的解釋我做錯了我會非常感激!
哪裏是**靜態GamePropertiesManager的定義*的GetInstance()**? – DumbCoder 2012-04-19 13:04:10