不能声明其他类的Class字段类型

时间:2013-09-14 09:45:32

标签: c++ class

为什么我不能声明其他类的Class字段类型?这给了我C4430错误:

//Entity.h file
    class Entity
    {
    public:
        Box test;
    };


    class Box
    {
    public:
        double length;   // Length of a box
        double breadth;  // Breadth of a box
        double height;   // Height of a box
    };

1 个答案:

答案 0 :(得分:3)

Entity需要在定义之前了解类Box。此外,由于您在Box类中包含了Entity的对象而不是指针,因此它还需要知道class Box的大小(Box的完整定义需要}类和成员的定义(因为它将访问Box::Box以初始化实际字段),因此在将Box作为Entity中的字段之前,您需要完整定义 class Box { public: double length; // Length of a box double breadth; // Breadth of a box double height; // Height of a box }; class Entity { public: Box test; }; {1}}课程。

{{1}}