错误:期望的类名'{'token [英] error: expected class-name before ‘{’ token

查看:122
本文介绍了错误:期望的类名'{'token的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道有几个类似的问题(循环包括)out stackoverflow和其他网站。但我还是不能想出来,没有解决方案弹出。所以我想发布我的具体一个。

I know there are a couple of similar questions(circular include) out stackoverflow and other websites. But I still can't figure it out and no solutions pop out. So I would like to post my specific one.

我有一个Event类有2个,实际上更多的子类,是Arrival和Landing。编译器(g ++)提示:

I have a Event class who has 2 and actually more subclass, which are Arrival and Landing. The compiler(g++) complains:

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)如下:

People said that it's a circular include. The 3 header files(Event.h Arrival.h Landing.h) are as follows:

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:

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

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_ */

更新:

我包括Landing.h,因为Landing的构造函数在Event :: createNewLanding方法中被调用:

I included Landing.h due to Landing's constructor is called in the Event::createNewLanding method:

Landing* Event::createNewLanding(Arrival* arrival) {
    return new Landing(flight, time + Landing::PERMISSION_TIME);
}


推荐答案

替换

#include "Landing.h"

class Landing;

如果您仍然遇到错误,还可以发布 Item.h Flight.h common.h

If you still get errors, also post Item.h, Flight.h and common.h

编辑:回应评论。

您需要使用例如 #includeLanding.h Event.cpp ,以便实际使用该类。您只能不包括 Event.h

You will need to e.g. #include "Landing.h" from Event.cpp in order to actually use the class. You just cannot include it from Event.h

这篇关于错误:期望的类名'{'token的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆