代码::具有 SFML 2,4 的块中未定义的引用
Undefined reference in Code::Blocks with SFML 2,4
我目前正在尝试按照此页面的说明使用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已经指出的那样,gameState
和mainWindow
丢失了。
实际上,它们在教程的第 2 部分页面上提到,就在游戏.cpp部分
的底部:Game::GameState Game::_gameState = Uninitialized;
sf::RenderWindow Game::_mainWindow;
我必须删除下划线才能使其正常工作。
相关文章:
- 编译C++时未定义的引用
- 如何修复此错误:未定义对"距离(浮点数,浮点数,浮点数,浮点数,浮点数)"的引用
- 我的项目不会像"undefined reference to `grpc::g_core_codegen_interface'"那样使用未定义的引用错误进行编译
- 对C宏的未定义引用,但在定义它时会出现重新定义错误
- 未定义的引用在哪里
- 编译时的 CImg 库返回对"__imp_SetDIBitsToDevice"的未定义引用
- 对Py_Initialize()的未定义引用
- 使用mysql c++连接器的未定义引用
- 未定义的引用 .. 使用 OpenCV 编译 C++ 代码时,从命令行
- 对 Scalar ::Scalar() 的未定义引用
- 已定义函数时出现 G++ "未定义的引用"错误
- 链接 cmake 时出现未定义的引用错误
- 对复制 CTOR 和 CTOR 的未定义引用
- 错误:未定义对'oboe::AudioStreamBuilder::openStream(oboe::AudioStream**)'的引用
- 右值引用未定义的行为
- 对"vtable for DigitalClock"的未定义引用 - 未定义对"DigitalClock::staticMetaObject"的引用 - Qt
- 函数类型到函数类型的引用:未定义的引用
- avcodec_alloc_context引用未定义,但ffmpeg链接顺序正确
- 从c++调用Fortran子程序,链接时引用未定义
- 枚举引用未定义,但位于包含的头文件中