代码::具有 SFML 2,4 的块中未定义的引用

Undefined reference in Code::Blocks with SFML 2,4

本文关键字:引用 未定义 具有 SFML 代码      更新时间:2023-10-16

我目前正在尝试按照此页面的说明使用C++和SFML构建一个小游戏:http://www.gamefromscratch.com/page/Game-From-Scratch-CPP-Edition-The-Introduction.aspx。 我不断遇到以下错误:

||=== Build: Debug in Pong (compiler: GNU GCC Compiler) ===|
G:AshwinWIPPongGame.cpp|34|warning: enumeration value 'Uninitialized' not handled in switch [-Wswitch]|
G:AshwinWIPPongGame.cpp|34|warning: enumeration value 'ShowingSplash' not handled in switch [-Wswitch]|
G:AshwinWIPPongGame.cpp|34|warning: enumeration value 'Paused' not handled in switch [-Wswitch]|
G:AshwinWIPPongGame.cpp|34|warning: enumeration value 'ShowingMenu' not handled in switch [-Wswitch]|
G:AshwinWIPPongGame.cpp|34|warning: enumeration value 'Exiting' not handled in switch [-Wswitch]|
objDebugGame.o||In function `ZN4Game5startEv':|
G:AshwinWIPPongGame.cpp|6|undefined reference to `Game::gameState'|
G:AshwinWIPPongGame.cpp|9|undefined reference to `Game::mainWindow'|
G:AshwinWIPPongGame.cpp|10|undefined reference to `Game::gameState'|
G:AshwinWIPPongGame.cpp|17|undefined reference to `Game::mainWindow'|
objDebugGame.o||In function `ZN4Game9isExitingEv':|
G:AshwinWIPPongGame.cpp|22|undefined reference to `Game::gameState'|
objDebugGame.o||In function `ZN4Game8gameLoopEv':|
G:AshwinWIPPongGame.cpp|34|undefined reference to `Game::gameState'|
G:AshwinWIPPongGame.cpp|38|undefined reference to `Game::mainWindow'|
G:AshwinWIPPongGame.cpp|39|undefined reference to `Game::mainWindow'|
G:AshwinWIPPongGame.cpp|43|undefined reference to `Game::gameState'|
G:AshwinWIPPongGame.cpp|31|undefined reference to `Game::mainWindow'|
||error: ld returned 1 exit status|
||=== Build failed: 11 error(s), 5 warning(s) (0 minute(s), 1 second(s)) ===|

以下是所有源文件的列表:

乒乓球.cpp

#include <iostream>
#include "Game.h"
int main(int argc, char** argv)
{
Game::start();
return 0;
}

游戏.cpp

#include <iostream>
#include "Game.h"
void Game::start(void)
{
if(gameState != Uninitialized)
return;
mainWindow.create(sf::VideoMode(1024,768,32),"Pang!");
gameState = Game::Playing;
while(!isExiting())
{
gameLoop();
}
mainWindow.close();
}
bool Game::isExiting()
{
if(gameState == Game::Exiting)
return true;
else
return false;
}
void Game::gameLoop()
{
sf::Event currentEvent;
while(mainWindow.pollEvent(currentEvent))
{   
switch(gameState)
{
case Game::Playing:
{
mainWindow.clear(sf::Color(255,0,0));
mainWindow.display();
if(currentEvent.type == sf::Event::Closed)
{
gameState = Game::Exiting;
}
break;
}
}
}
}

游戏.h

#ifndef GAME_H_INCLUDED
#define GAME_H_INCLUDED
#include <SFML/Graphics.hpp>
#include <SFML/Window.hpp>
class Game
{
public:
static void start();
private:
static bool isExiting();
static void gameLoop();
enum GameState {Uninitialized,ShowingSplash,Paused,ShowingMenu,Playing,Exiting};
static GameState gameState;
static sf::RenderWindow mainWindow;
};
#endif // GAME_H_INCLUDED

如何更正这些错误?

让我把这个答案从评论中放在这里。正如@DeiDei已经指出的那样,gameStatemainWindow丢失了。

实际上,它们在教程的第 2 部分页面上提到,就在游戏.cpp部分

的底部:
Game::GameState Game::_gameState = Uninitialized; 
sf::RenderWindow Game::_mainWindow; 

我必须删除下划线才能使其正常工作。