在c ++中定义枚举类型的静态变量

时间:2012-06-08 14:43:45

标签: c++

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并给我错误

2 个答案:

答案 0 :(得分:4)

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

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

希望它有所帮助!

答案 1 :(得分:2)

因为enum GameState包含在class Game中,您需要相应地调整变量的范围:

Game::GameState Game::_gameState;