我知道有幾個類似的問題(通告包括)出棧計算器和其他網站。但我仍然無法弄清楚,沒有解決方案。所以我想發佈我的具體內容。錯誤:在'{'令牌之前預期的類名
我有一個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_ */
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_ */
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_ */
UPDATE:
我公司luded Landing.h由於着陸的構造函數被調用的事件:: createNewLanding方法:
Landing* Event::createNewLanding(Arrival* arrival) {
return new Landing(flight, time + Landing::PERMISSION_TIME);
}
根據編譯器輸出,你的錯誤在'Landing.h'中(在第13行)。爲什麼你在'Event.h'中發表評論說錯誤在那裏? – 2011-03-16 01:13:43
@Ben Voigt對不起,我改變了它 – draw 2011-03-16 01:21:22