gpt4 book ai didi

c++ - LNK 2001 单例上未解析的外部符号

转载 作者:行者123 更新时间:2023-11-28 06:21:27 28 4
gpt4 key购买 nike

<分区>

我正在使用 SFML 和 Box2D 创建一个类似 peggle 的游戏,但是我的游戏单例出现了这个错误。

Error 1 error LNK2001: 未解析的外部符号“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;
}

游戏.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;
};

游戏.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;
}

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com