我有一个静态类,我正在使用我的状态驱动游戏,在使用XCODE编译时,我一直收到此链接器错误:
Undefined symbols for architecture x86_64:
"Game::_gameState", referenced from:
Game::Start() in Game.o
Game::IsExiting() in Game.o
Game::GameLoop() in Game.o
"Game::_mainWindow", referenced from:
Game::Start() in Game.o
Game::GameLoop() in Game.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
我对C ++的理解非常有限,每当我尝试使用它时,我总会发现自己遇到了奇怪的怪癖。我不确定如何定义这些符号并将其保持为静态类。我试图在类定义之外定义它们没有运气,我做错了什么?
这是来源:
Game.hpp
#include <SFML/Graphics.hpp>
#include <SFML/Audio.hpp>
class Game
{
public:
static void Start();
static bool IsExiting();
static void GameLoop();
enum GameState { Uninitialized, ShowingSplash,
Paused, ShowingMenu, Playing, Exiting };
static GameState _gameState;
static sf::RenderWindow _mainWindow;
};
Game.cpp
#include "Game.hpp"
void Game::Start()
{
if(_gameState != Uninitialized)
return;
_mainWindow.create(sf::VideoMode(1024, 768, 32), "This game!");
_mainWindow.setVerticalSyncEnabled(true);
_gameState = Playing;
while(!IsExiting())
{
GameLoop();
}
_mainWindow.close();
}
bool Game::IsExiting()
{
if(_gameState == Exiting)
return true;
else
return false;
}
void Game::GameLoop()
{
sf::Event currentEvent;
while(_mainWindow.pollEvent(currentEvent))
{
switch(_gameState)
{
case Playing:
{
_mainWindow.clear(sf::Color::Black);
_mainWindow.display();
if(currentEvent.type == sf::Event::Closed)
{
_gameState = Game::Exiting;
}
break;
}
}
}
}
答案 0 :(得分:2)
您需要在static
声明之外定义class
成员:
Game::GameState Game::_gameState;
sf::RenderWindow Game::_mainWindow;
使用所有这些public
static
方法,Game
可能不应该是 class
,而是 {{1} } 的。