【发布时间】:2021-09-22 04:25:41
【问题描述】:
我知道在 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
人们说这是一个循环包含。 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_ */
到达.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_ */
着陆.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_ */
更新:
我包含 Landing.h 是因为在 Event::createNewLanding 方法中调用了 Landing 的构造函数:
Landing* Event::createNewLanding(Arrival* arrival) {
return new Landing(flight, time + Landing::PERMISSION_TIME);
}
【问题讨论】:
-
根据编译器输出,您的错误在
Landing.h(第 13 行)。你为什么要在Event.h中发表评论说存在错误? -
@Ben Voigt 抱歉,我已经改了