1

为什么我不能声明其他类的类字段类型?这给了我 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
    };
4

1 回答 1

3

Class需要在定义之前Entity了解 class 。Box此外,当您在类中包含对象而不是指针时BoxEntity它还需要知道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;
    };
于 2013-09-14T09:46:49.407 回答