1
class Game
{
public:
    static void Start();

private:
    static bool IsExiting();
    static void GameLoop();

    static void ShowSplashScreen();
    static void ShowMenu();

    enum GameState { Uninitialized, ShowingSplash, Paused,
                    ShowingMenu, Playing, Exiting };

    Game::GameState Game::_gameState = Game::Uninitialized;
    static sf::RenderWindow _mainWindow;
};
Game::GameState Game::_gameState;
RenderWindow Game::_mainWindow;

错误现在在首先运行的函数中

void processEvents(){// error: first defined here ???
    Event event;
    while(App.pollEvent(event)){
        if(event.type == Event::Closed)
                App.close();
    }
}

还有其他错误,例如在函数中定义了多个 `Game::_gameState' 它在 Game 类中声明的其他函数中给出了相同的错误

void Game::Start(void) { // multiple definition of `Game::_gameState'
    if (_gameState != Uninitialized)
        return;

    _mainWindow.create(VideoMode(1200, 900, 32), "Game",
            Style::Close);
    _gameState = Game::ShowingSplash;

    while (!IsExiting()) {
        GameLoop();
    }

    _mainWindow.close();
}

我修复了错误,但又出现了一个

while (!IsExiting()) {  //undefined reference to Game::IsExiting
    GameLoop();
}

_mainWindow.close();

}

我不知道它是静态 bool 函数,其他函数可以正常工作,但这个是 bool 并给我错误

4

2 回答 2

4

GameStateenum 在类的范围内声明Game。因此,当您在该类的范围之外引用它时,您必须使用完全限定的类型名称(这包括定义_gameState字段的实例。我还建议您给它一些初始值。例如:

Game::GameState Game::_gameState = Game::Uninitialized;

希望能帮助到你!

于 2012-06-08T14:47:48.730 回答
2

因为enum GameState包含在class Game您需要相应地限定变量的范围内:

Game::GameState Game::_gameState;
于 2012-06-08T14:46:35.233 回答