所以,我有点期待这最终成为一个简单的答案,但我现在已经黑了一段时间了,似乎无法解决这个问题.所以我有一个特定的类,Intersection
当包含在任何其他标题中时,我会:
error C2061: syntax error : identifier 'Intersection'
这是我的交叉头:
#ifndef INTERSECTION_H
#define INTERSECTION_H
#include "Coord.h"
#include "Road.h"
#include "TrafficLight.h"
class Intersection {
private:
int id;
Coord * midPoint;
Road * northRoad;
Road * eastRoad;
Road * westRoad;
Road * southRoad;
TrafficLight * trafficLight;
public:
Intersection(int, Coord *, Road *, Road *, Road *, Road *);
~Intersection();
void transitionTrafficLight();
int getId();
Road * getNorthRoad();
Road * getEastRoad();
Road * getWestRoad();
Road * getSouthRoad();
TrafficLight * getTrafficLight();
};
#endif
Run Code Online (Sandbox Code Playgroud)
现在,如果我尝试在其他地方使用此类,我会收到错误.例如:
#ifndef ROAD_H
#define ROAD_H
#include "Coord.h"
#include "Intersection.h"
#include <string>
class Road {
public:
enum LaneCount { TWO_LANE = 2, FOUR_LANE = 4 };
Road(std::string, Coord *, Coord *, LaneCount, Intersection *, Intersection *, int);
//shortened
Run Code Online (Sandbox Code Playgroud)
特别是在Road
构造函数(以及任何其他引用的类Intersection
).我不认为这是一个语法问题,因为Coord
另一个类,以相同的方式定义,并且编译器(VS 2008)不会抱怨它.这只是Intersection
在特定的多数民众赞成给我这个麻烦.:/
我正在标记它的作业 - 这就是它的用途,即使这只是一个错误,我无法摆脱而不是问题的一部分.
思考?
tem*_*def 26
看起来错误的是你有两个循环包含的头文件 - intersection.h和road.h. 这样做往往会导致C++出现奇怪的意外,因为包括警卫的工作方式.例如,假设我有两个头文件,如下所示:
// File: A.h
#ifndef A_Included
#define A_Included
#include "B.h"
class A {};
void MyFunction(B argument);
#endif
Run Code Online (Sandbox Code Playgroud)
和
// File: B.h
#ifndef B_Included
#define B_Included
#include "A.h"
class B {};
void MyOtherFunction(A argument);
#endif
Run Code Online (Sandbox Code Playgroud)
现在,如果我尝试#include "A.h"
,那么它会扩展到
// File: A.h
#ifndef A_Included
#define A_Included
#include "B.h"
class A {};
void MyFunction(B argument);
#endif
Run Code Online (Sandbox Code Playgroud)
当我尝试扩展时#include "B.h"
,我得到了这个:
// File: A.h
#ifndef A_Included
#define A_Included
// File: B.h
#ifndef B_Included
#define B_Included
#include "A.h"
class B {};
void MyOtherFunction(A argument);
#endif
class A {};
void MyFunction(B argument);
#endif
Run Code Online (Sandbox Code Playgroud)
此时,预处理器将再次尝试扩展A.h
,这导致:
// File: A.h
#ifndef A_Included
#define A_Included
// File: B.h
#ifndef B_Included
#define B_Included
// File: A.h
#ifndef A_Included
#define A_Included
#include "B.h"
class A {};
void MyFunction(B argument);
#endif
class B {};
void MyOtherFunction(A argument);
#endif
class A {};
void MyFunction(B argument);
#endif
Run Code Online (Sandbox Code Playgroud)
现在,让我们看看当我们解决所有这些奇怪的包括警卫时会发生什么.我们第一次看到A时,它已经扩展了,就像我们第一次扩展B的情况一样.但是,当我们第二次看到A时,它根本没有展开.因此,在取出注释和预处理器指令之后,我们得到了这个结果代码:
class B {};
void MyOtherFunction(A argument);
class A {};
void MyFunction(B argument);
Run Code Online (Sandbox Code Playgroud)
请注意,MyOtherFunction
声明时,A
尚未声明,因此编译器报告错误.
要解决此问题,您可以在需要它们的头文件中转发声明A
和B
:
// File: A.h
#ifndef A_Included
#define A_Included
class A {};
class B; // Forward declaration
void MyFunction(B argument);
#endif
Run Code Online (Sandbox Code Playgroud)
和
// File: B.h
#ifndef B_Included
#define B_Included
class B {};
class A; // Forward declaration
void MyFunction(B argument);
#endif
Run Code Online (Sandbox Code Playgroud)
现在,没有更多的循环依赖.只要您#include
在.cpp文件中使用相应的头文件,就应该没问题.
希望这可以帮助!
归档时间: |
|
查看次数: |
11229 次 |
最近记录: |