0

I am getting flustrated with two errors and I have absolutely no idea what is wrong.

#ifndef ListElements
#define ListElements

#include "RentalObjects.h"

struct RentalList{
ObjectBase* content;
RentalList* Next;
};
#endif 

All the time I get this error:

Error 1 error C2143: syntax error : missing ';' before '*'

Error 2 error C4430: missing type specifier - int assumed.

The RentalObjects.h file features a declaration of the ObjectBase class, which looks as follows:

class ObjectBase{
protected:
    char Make[16];
    char Model[16];
    int Year;
    float PricePerDay;
    Booking* Availability;
public:
    void SetMake(char* value);
    void SetModel(char* value);
    void SetYear(int value);
    void SetPrice(float value);
    bool DisposeBookings();
    bool Book(int Start,int End);
    char* GetMake();
    char* GetModel();
    int GetYear();
    float GetPrice();
    ~ObjectBase();
};

I'd be grateful for a tip.

7
  • Most likely you missed a ; at the end of one of a class definition (or somewhere else) inside RentalObjects.h, or otherwise caused ObjectBase to be undeclared due to a syntax error Commented Dec 9, 2014 at 0:19
  • can we see the whole RentalObjects.h ? does it have include guards? does it include something else? maby recursive includes? Commented Dec 9, 2014 at 0:22
  • Sure, I had multiple classes in it but now I removed them, so take a look: pastebin.com/WQfJuCah Commented Dec 9, 2014 at 0:26
  • 1
    What about Booking. Is there a declaration for it? Commented Dec 9, 2014 at 0:32
  • What is this "Booking" thing? Learn to forward-declare but not define pointed-to classes. You don't need to know what a "Booking" to define your class ObjectBase. Similarly, you don't need to know what an "ObjectBase" is to define your class RentalList. Just forward declare. #including the files that define those classes will inevitably get you into a circular mess. Commented Dec 9, 2014 at 0:32

1 Answer 1

1

When declaring pointers or references, you don't need the whole class/struct definition.

Instead of:

#include "RentalObjects.h"

struct RentalList {
    ObjectBase* content;
    RentalList* Next;
};

you could do:

class ObjectBase;
struct RentalList {
    ObjectBase* content;
    RentalList* Next;
};

this could get you out of a circular include, which might be what's causing your broblem

Sign up to request clarification or add additional context in comments.

3 Comments

I did as you suggested and it sadly didn't help. Moreover all headers are #ifndef secured so I suppose it should get circularly included (I'm guessing).
make sure you are breaking all circular references. the include guard protects you from infinite loop in the compiler, but it still wont work, the second time a header tries to be included, it wont be, and the symbols wont be defined
Ok, you were right, it worked finally. Thanks a lot!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.