简体   繁体   中英

Class inside a struct

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM