简体   繁体   English

类内的结构

[英]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 ';' 错误1错误C2143:语法错误:缺少';' before '*' 在“ *”之前

Error 2 error C4430: missing type specifier - int assumed. 错误2错误C4430:缺少类型说明符-假定为int。

The RentalObjects.h file features a declaration of the ObjectBase class, which looks as follows: RentalObjects.h文件具有ObjectBase类的声明,该声明如下所示:

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 这可能会使您脱离循环包含,这可能是导致您出现问题的原因

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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