我知道有一些类似的问题(循环包括)stackoverflow和其他网站.但我仍然无法弄明白,也没有解决方案.所以我想发布我的具体内容.
我有一个Event类,它有2个实际上更多的子类,它们是Arrival和Landing.编译器(g ++)抱怨:
g++ -c -Wall -g -DDEBUG Event.cpp -o Event.o
In file included from Event.h:15,
from Event.cpp:8:
Landing.h:13: error: expected class-name before ‘{’ token
make: *** [Event.o] Error 1
Run Code Online (Sandbox Code Playgroud)
人们说这是一个循环包括.3个头文件(Event.h Arrival.h Landing.h)如下:
Event.h:
#ifndef EVENT_H_
#define EVENT_H_
#include "common.h"
#include "Item.h"
#include "Flight.h"
#include "Landing.h"
class Arrival;
class Event : public Item {
public:
Event(Flight* flight, int time);
virtual ~Event();
virtual void occur() = 0;
virtual string extraInfo() = 0; // extra info for each concrete event
// @implement
int compareTo(Comparable* b);
void print();
protected:
/************** this is why I wanna include Landing.h *******************/
Landing* createNewLanding(Arrival* arrival); // return a Landing obj based on arrival's info
private:
Flight* flight;
int time; // when this event occurs
};
#endif /* EVENT_H_ */
Run Code Online (Sandbox Code Playgroud)
Arrival.h:
#ifndef ARRIVAL_H_
#define ARRIVAL_H_
#include "Event.h"
class Arrival: public Event {
public:
Arrival(Flight* flight, int time);
virtual ~Arrival();
void occur();
string extraInfo();
};
#endif /* ARRIVAL_H_ */
Run Code Online (Sandbox Code Playgroud)
Landing.h
#ifndef LANDING_H_
#define LANDING_H_
#include "Event.h"
class Landing: public Event {/************** g++ complains here ****************/
public:
static const int PERMISSION_TIME;
Landing(Flight* flight, int time);
virtual ~Landing();
void occur();
string extraInfo();
};
#endif /* LANDING_H_ */
Run Code Online (Sandbox Code Playgroud)
更新:
由于Landing的构造函数在Event :: createNewLanding方法中调用,因此我包含了Landing.h:
Landing* Event::createNewLanding(Arrival* arrival) {
return new Landing(flight, time + Landing::PERMISSION_TIME);
}
Run Code Online (Sandbox Code Playgroud)
Ben*_*igt 80
这应该是注释,但注释不允许多行代码.
这是发生了什么:
在 Event.cpp
#include "Event.h"
Run Code Online (Sandbox Code Playgroud)
预处理器开始处理 Event.h
#ifndef EVENT_H_
Run Code Online (Sandbox Code Playgroud)
它还没有定义,所以继续
#define EVENT_H_
#include "common.h"
Run Code Online (Sandbox Code Playgroud)
common.h 处理好了
#include "Item.h"
Run Code Online (Sandbox Code Playgroud)
Item.h 处理好了
#include "Flight.h"
Run Code Online (Sandbox Code Playgroud)
Flight.h 处理好了
#include "Landing.h"
Run Code Online (Sandbox Code Playgroud)
预处理器开始处理 Landing.h
#ifndef LANDING_H_
Run Code Online (Sandbox Code Playgroud)
尚未定义,继续前进
#define LANDING_H_
#include "Event.h"
Run Code Online (Sandbox Code Playgroud)
预处理器开始处理 Event.h
#ifndef EVENT_H_
Run Code Online (Sandbox Code Playgroud)
这已经定义了,文件的其余部分都被跳过了.继续Landing.h
class Landing: public Event {
Run Code Online (Sandbox Code Playgroud)
预处理器并不关心这一点,但编译器会说"WTH是Event?我还没有听说过Event."
Eri*_*rik 25
更换
#include "Landing.h"
Run Code Online (Sandbox Code Playgroud)
同
class Landing;
Run Code Online (Sandbox Code Playgroud)
如果你仍然得到错误,也发布Item.h,Flight.h和common.h
编辑:回应评论.
您将需要例如#include "Landing.h"从Event.cpp实际使用该类.你不能包含它Event.h
| 归档时间: |
|
| 查看次数: |
153939 次 |
| 最近记录: |