我在微软Visual Studio 2010中用c ++编写游戏,昨天我写了一个乒乓球游戏,一切都很好,但现在编译器告诉我有很多错误,例如:
1>w:\c++\planet escape\planet escape\room.h(25): error C2061: syntax error : identifier 'WorldMap'
这是Room.h文件:
#pragma once
#include <allegro5/allegro.h>
#include <vector>
#include "Entity.h"
#include "WorldMap.h"
#include "Link.h"
#define ROOM_W 20
#define ROOM_H 20
class Room{
private:...
public:...
};
在代码中没有错误,它看到所有类都很好。 那么什么会导致这样的错误?
编辑: 这是WorldMap.h
#pragma once
#include <allegro5/allegro.h>
#include "Room.h"
#include "Player.h"
#define WORLD_W 10
#define WORLD_H 10
class WorldMap{
private:...
public:...
};
如果我在运行它时,他无法看到它为何在编码时看到它?
答案 0 :(得分:5)
您有循环包含。假设您正在编译一个#include "WorldMap.h"
作为第一个适用的#include
语句的文件。文件WorldMap.h
具有#include "Room.h"
,这将导致很多麻烦。问题始于Room.h
#include "WorldMap.h"
声明中的问题。由于#include
中的#pragma once
,WorldMap.h
无效。当编译器到达处理Room.h
主体的点时,类WorldMap
既未定义也未声明。
<强>附录强>
解决方案是摆脱那些无关的#include
语句。文件WorldMap.h
不需要#include
或Room.h
Player.h
。相反,它需要对类Room
和Player
进行前向声明。同样,您也不需要#include
中的所有Room.h
语句。
一般来说,最好在标题中使用类型的前向声明,而不是包含定义类型的文件。如果标题中的代码不需要知道相关类型的详细信息,只需使用前向声明即可。不要#include
定义类型的标题。