我目前正在尝试使用C ++和SFML按照此页面的说明构建一个小游戏:http://www.gamefromscratch.com/page/Game-From-Scratch-CPP-Edition-The-Introduction.aspx。 我一直遇到以下错误:
||=== Build: Debug in Pong (compiler: GNU GCC Compiler) ===|
G:\Ashwin\WIP\Pong\Game.cpp|34|warning: enumeration value 'Uninitialized' not handled in switch [-Wswitch]|
G:\Ashwin\WIP\Pong\Game.cpp|34|warning: enumeration value 'ShowingSplash' not handled in switch [-Wswitch]|
G:\Ashwin\WIP\Pong\Game.cpp|34|warning: enumeration value 'Paused' not handled in switch [-Wswitch]|
G:\Ashwin\WIP\Pong\Game.cpp|34|warning: enumeration value 'ShowingMenu' not handled in switch [-Wswitch]|
G:\Ashwin\WIP\Pong\Game.cpp|34|warning: enumeration value 'Exiting' not handled in switch [-Wswitch]|
obj\Debug\Game.o||In function `ZN4Game5startEv':|
G:\Ashwin\WIP\Pong\Game.cpp|6|undefined reference to `Game::gameState'|
G:\Ashwin\WIP\Pong\Game.cpp|9|undefined reference to `Game::mainWindow'|
G:\Ashwin\WIP\Pong\Game.cpp|10|undefined reference to `Game::gameState'|
G:\Ashwin\WIP\Pong\Game.cpp|17|undefined reference to `Game::mainWindow'|
obj\Debug\Game.o||In function `ZN4Game9isExitingEv':|
G:\Ashwin\WIP\Pong\Game.cpp|22|undefined reference to `Game::gameState'|
obj\Debug\Game.o||In function `ZN4Game8gameLoopEv':|
G:\Ashwin\WIP\Pong\Game.cpp|34|undefined reference to `Game::gameState'|
G:\Ashwin\WIP\Pong\Game.cpp|38|undefined reference to `Game::mainWindow'|
G:\Ashwin\WIP\Pong\Game.cpp|39|undefined reference to `Game::mainWindow'|
G:\Ashwin\WIP\Pong\Game.cpp|43|undefined reference to `Game::gameState'|
G:\Ashwin\WIP\Pong\Game.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)) ===|
以下是所有源文件的列表:
Pong.cpp
#include <iostream>
#include "Game.h"
int main(int argc, char** argv)
{
Game::start();
return 0;
}
Game.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;
}
}
}
}
Game.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
我怎样才能纠正这些错误?
答案 0 :(得分:0)
让我从这里的评论中回答这个问题。正如@DeiDei已经指出gameState
和mainWindow
缺失。
实际上它们在本教程的第2部分页面上提到,就在game.cpp部分的底部:
Game::GameState Game::_gameState = Uninitialized;
sf::RenderWindow Game::_mainWindow;
我必须删除下划线才能使其正常工作。