error: expected class-name before ‘{’ token

前端 未结 5 680
独厮守ぢ
独厮守ぢ 2020-11-30 05:41

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

相关标签:
5条回答
  • 2020-11-30 06:02

    I got the same error with a different problem,

    I used namespaces in my headers and forgot the closing bracket and got this cryptic error instead.

    0 讨论(0)
  • 2020-11-30 06:07

    If you forward-declare Flight and Landing in Event.h, then you should be fixed.

    Remember to #include "Flight.h" and #include "Landing.h" in your implementation file for Event.

    The general rule of thumb is: if you derive from it, or compose from it, or use it by value, the compiler must know its full definition at the time of declaration. If you compose from a pointer-to-it, the compiler will know how big a pointer is. Similarly, if you pass a reference to it, the compiler will know how big the reference is, too.

    0 讨论(0)
  • 2020-11-30 06:11

    I know it is a bit late to answer this question, but it is the first entry in google, so I think it is worth to answer it.

    The problem is not a coding problem, it is an architecture problem.

    You have created an interface class Event: public Item to define the methods which all events should implement. Then you have defined two types of events which inherits from class Event: public Item; Arrival and Landing and then, you have added a method Landing* createNewLanding(Arrival* arrival); from the landing functionality in the class Event: public Item interface. You should move this method to the class Landing: public Event class because it only has sense for a landing. class Landing: public Event and class Arrival: public Event class should know class Event: public Item but event should not know class Landing: public Event nor class Arrival: public Event.

    I hope this helps, regards, Alberto

    0 讨论(0)
  • 2020-11-30 06:12

    This should be a comment, but comments don't allow multi-line code.

    Here's what's happening:

    in Event.cpp

    #include "Event.h"
    

    preprocessor starts processing Event.h

    #ifndef EVENT_H_
    

    it isn't defined yet, so keep going

    #define EVENT_H_
    #include "common.h"
    

    common.h gets processed ok

    #include "Item.h"
    

    Item.h gets processed ok

    #include "Flight.h"
    

    Flight.h gets processed ok

    #include "Landing.h"
    

    preprocessor starts processing Landing.h

    #ifndef LANDING_H_
    

    not defined yet, keep going

    #define LANDING_H_
    
    #include "Event.h"
    

    preprocessor starts processing Event.h

    #ifndef EVENT_H_
    

    This IS defined already, the whole rest of the file gets skipped. Continuing with Landing.h

    class Landing: public Event {
    

    The preprocessor doesn't care about this, but the compiler goes "WTH is Event? I haven't heard about Event yet."

    0 讨论(0)
  • 2020-11-30 06:15

    Replace

    #include "Landing.h"
    

    with

    class Landing;
    

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

    EDIT: In response to comment.

    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

    0 讨论(0)
提交回复
热议问题