C++ LNK 2001单例上未解析的外部符号
我正在用SFML和Box2D创建一个类似peggle的游戏,但是我的游戏singleton出现了这个错误 错误1错误LNK2001:未解析的外部符号“私有:静态类游戏*游戏::s_pInstance”(?s_pInstance@Game@@0PAV1@A)C:\Users\UserName\Desktop\PeggleClone\Maze\Maze\Game.obj Main.cppC++ LNK 2001单例上未解析的外部符号,c++,linker,singleton,game-engine,C++,Linker,Singleton,Game Engine,我正在用SFML和Box2D创建一个类似peggle的游戏,但是我的游戏singleton出现了这个错误 错误1错误LNK2001:未解析的外部符号“私有:静态类游戏*游戏::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_WID
#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.cpp顶部添加这一行来初始化游戏实例
Game* Game::s_pInstance = nullptr;
此外,如果您确实需要,我建议您使用。
#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;
}
Game* Game::s_pInstance = nullptr;