C ++中的语法错误包括文件

时间:2011-08-20 15:40:13

标签: c++ visual-studio-2010 visual-c++

我在微软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:...
};

如果我在运行它时,他无法看到它为何在编码时看到它?

1 个答案:

答案 0 :(得分:5)

您有循环包含。假设您正在编译一个#include "WorldMap.h"作为第一个适用的#include语句的文件。文件WorldMap.h具有#include "Room.h",这将导致很多麻烦。问题始于Room.h #include "WorldMap.h"声明中的问题。由于#include中的#pragma onceWorldMap.h无效。当编译器到达处理Room.h主体的点时,类WorldMap既未定义也未声明。

<强>附录
解决方案是摆脱那些无关的#include语句。文件WorldMap.h不需要#includeRoom.h Player.h。相反,它需要对类RoomPlayer进行前向声明。同样,您也不需要#include中的所有Room.h语句。

一般来说,最好在标题中使用类型的前向声明,而不是包含定义类型的文件。如果标题中的代码不需要知道相关类型的详细信息,只需使用前向声明即可。不要#include定义类型的标题。

相关问题