Clang链接C ++静态类的错误:未定义的符号

时间:2013-12-07 07:41:32

标签: c++ xcode linker sfml

我有一个静态类,我正在使用我的状态驱动游戏,在使用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;
            }
        }
    }
}

1 个答案:

答案 0 :(得分:2)

您需要在static声明之外定义class成员:

在Game.cpp中:

Game::GameState Game::_gameState;
sf::RenderWindow Game::_mainWindow;

使用所有这些public static方法,Game可能不应该是 class ,而是 {{1} } 的。