C++ 错误:在‘;之前应为类名;{&x2019;代币

C++ 错误:在‘;之前应为类名;{&x2019;代币,c++,g++,C++,G++,我知道stackoverflow和其他网站上也有一些类似的问题(包括循环问题)。但我仍然无法解决,也没有解决方案。所以我想发布我的具体问题 我有一个事件类,它有2个甚至更多的子类,它们是到达和着陆。编译器(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: expe

我知道stackoverflow和其他网站上也有一些类似的问题(包括循环问题)。但我仍然无法解决,也没有解决方案。所以我想发布我的具体问题

我有一个事件类,它有2个甚至更多的子类,它们是到达和着陆。编译器(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):

事件。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_ */
着陆

#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);
}
替换

#include "Landing.h"

如果仍然出现错误,也可以发布
Item.h
Flight.h
common.h

编辑:回应评论


例如,您需要从
Event.cpp
中包含“Landing.h”,以便实际使用该类。您不能从
Event.h

中包含该类。如果您在
Event.h
中向前申报
航班
着陆
,则您应该得到修复

#ifndef EVENT_H_
#ifndef EVENT_H_
记住在
事件
的实现文件中包含“Flight.h”和
并包含“Landing.h”


一般的经验法则是:如果您从它派生,或从它合成,或按值使用它,编译器在声明时必须知道它的完整定义。如果您从指向它的指针合成,编译器将知道指针有多大。同样,如果您向它传递引用,编译器也将知道引用有多大

这应该是注释,但注释不允许多行代码

下面是正在发生的事情:

Event.cpp中

#include "Event.h"
预处理器开始处理
Event.h

#ifndef EVENT_H_
#ifndef EVENT_H_
它还没有定义,所以继续

#define EVENT_H_
#include "common.h"
#define LANDING_H_

#include "Event.h"
common.h
处理正常

#include "Item.h"
#include "Flight.h"
#include "Landing.h"
Item.h
处理正常

#include "Item.h"
#include "Flight.h"
#include "Landing.h"
Flight.h
处理正常

#include "Item.h"
#include "Flight.h"
#include "Landing.h"
预处理器开始处理
Landing.h

#ifndef LANDING_H_
class Landing: public Event {
还没有定义,继续

#define EVENT_H_
#include "common.h"
#define LANDING_H_

#include "Event.h"
预处理器开始处理
Event.h

#ifndef EVENT_H_
#ifndef EVENT_H_
这已定义,将跳过文件的其余部分。继续执行
Landing.h

#ifndef LANDING_H_
class Landing: public Event {

预处理器并不关心这一点,但编译器会说“What is
Event
?我还没有听说过
Event

我在不同的问题上遇到了相同的错误


我在标题中使用了名称空间,但忘记了右括号,而是得到了这个神秘的错误。

我知道回答这个问题有点晚,但这是谷歌的第一个条目,所以我认为值得回答它

问题不是编码问题,而是架构问题

您已经创建了一个接口
class Event:public Item
来定义所有事件应该实现的方法。然后您定义了从
class Event继承的两种类型的事件:public Item
;Arrival和Landing,然后,您添加了一个方法
Landing*createNewLanding(Arrival*Arrival)
类事件:公共项界面中的登陆功能。您应该将此方法移动到
类登陆:公共事件
类,因为它只对登陆有意义。
类登陆:公共事件
类到达:公共事件
类应该知道
类事件:公共物品
但事件不应知道
类着陆:公共事件
类到达:公共事件


我希望这会有所帮助,Alberto

根据编译器的输出,您的错误在
Landing.h
(第13行)。为什么您在
Event.h
中添加注释说错误在那里?@Ben Voigt抱歉,我已经更改了
#include
通常不正确-使用
“privateheader.h”
因为我调用了Landing的构造函数:Landing*Event::createNewLanding(Arrival*Arrival){返回新的Landing(flight,time+Landing::PERMISSION_time);}
#在.cpp中包含所需的头文件,而不是.ht。这很有效。非常感谢。另一个问题:这是否意味着在头文件中始终进行前言引用是一种好做法?@draw:当转发声明足够时,转发声明是一种好做法(您只需要
类型*
类型&
)-除了避免这种循环包含问题外,这还减少了文件之间的相互依赖性,从而减少了a.o.的编译时间。请听评论!将
#include
放在.cpp中,而不是放在.h中!这是我修复它的原因。您仍然需要确保.h文件是
\include
d,以便由o定义的文件它们的位置较低。此外,正如前面的注释所述,当循环定义任何内容时,您可以使用类的前向声明来提供帮助。@Ben如果我们使用
类登录
,而不是
,请包括“Landing.h”
着陆
会被定义两次吗?如果预处理器不跳过
#ifndef着陆
,它如何处理
类着陆
?定义与否?@MiloLu:这就是为什么使用前向声明
类着陆;
,它不是定义(在
{/code>中没有主体)。