最近我在使用Visual C++ 2012时遇到了一个非常令人沮丧的问题。直到几个小时前,我一直能够正常编写代码并且一切都按照预期运行,直到我决定对一些内容进行优化并删除了一些类。我已经修复了由此引起的所有错误,例如错误的包含等等。不幸的是,之后VS编译器开始出现各种错误,例如:
Error 14 error C2653: 'Class' : is not a class or namespace name
或者甚至更多
Error 5 error C2143: syntax error : missing ';' before '}'
Error 4 error C2059: syntax error : '>'
我已经检查了多次,一切都在正确的位置:所有头文件都被包含了,所有符号也都放在了它们应该在的地方。
据我所知,问题不在我的代码上,而是与编译器本身有关......Visual Studio 有时可能真的很烦人。无论如何,如果有人能帮我解决这个问题,我会非常感激。
(顺便说一下,禁用预编译头文件并没有起作用)
相关代码片段:
Error 14:
#include "PlayerEntity.h"
PlayerEntity::PlayerEntity(void) {} // This line causes the error
错误5:
class GameScreen : public BaseScreen
{
public:
...
private:
...
}; // This line causes the error
错误 4:
private:
std::vector<BaseEntity*> _EntityList; // This line causes the error
整个PlayerEntity.h文件:
#ifndef PENTITY_H
#define PENTITY_H
#include "BaseEntity.h"
class PlayerEntity : public BaseEntity
{
public:
PlayerEntity(void);
PlayerEntity(float, float);
virtual ~PlayerEntity(void);
void render(sf::RenderWindow&);
void update();
private:
void init();
};
#endif
整个GameScreen.h文件:
#ifndef GSCREEN_H
#define GSCREEN_H
#include "BaseScreen.h"
#include "BaseEntity.h"
#include "PlayerEntity.h"
class GameScreen : public BaseScreen
{
public:
GameScreen(sf::Vector2u&);
virtual ~GameScreen(void);
void start();
void stop();
void render(sf::RenderWindow&);
void update(void);
void addEntity(BaseEntity*);
void destoryEntity(int id);
private:
std::vector<BaseEntity*> _EntityList;
sf::Vector2u _ScreenDimensions;
};
#endif
整个 BaseEntity.h 文件:
#ifndef BSENTITY_H
#define BSENTITY_H
#include "Input.h"
#include <SFML/Graphics.hpp>
class BaseEntity
{
public:
BaseEntity(void);
virtual ~BaseEntity(void);
sf::Vector2f position;
virtual void update(void);
virtual void render(sf::RenderWindow&);
void compare(BaseEntity*);
protected:
sf::Texture *_EntityTexture;
sf::Sprite _EntitySprite;
bool _isAlive;
int _id;
virtual void init();
};
#endif
整个Input.h文件:
#ifndef INPUT_H
#define INPUT_H
#include "ScreenSystem.h"
#include <SFML/Window.hpp>
class Input
{
public:
Input(void);
Input(sf::RenderWindow*);
virtual ~Input(void);
static bool keyPressed(int);
static bool keyReleased(int);
static bool mouseHeld(int);
static bool mouseReleased(int);
private:
static sf::RenderWindow *_Window;
};
#endif
整个ScreenSystem.h文件:
#ifndef GHANDLER_H
#define GHANDLER_H
#include "BaseScreen.h"
#include "MenuScreen.h"
#include "GameScreen.h"
#include <SFML/Window.hpp>
class ScreenSystem
{
public:
ScreenSystem(void);
ScreenSystem(sf::RenderWindow*);
virtual ~ScreenSystem(void);
BaseScreen *getCurrentScreen(void);
void setScreen(int);
private:
int _currentScreenID;
std::vector<BaseScreen*> _Screens;
sf::RenderWindow *_Window;
};
#endif
;
的错误往往是编译器遇到无法识别的标记(比如,一个尚未声明的类名)的症状。 - jamesdlin