gpt4 book ai didi

c++ - 无法声明其他类的类字段类型

转载 作者:行者123 更新时间:2023-11-30 02:52:31 25 4
gpt4 key购买 nike

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

最佳答案

Entity需要了解类(class) Box , 在定义之前。此外,因为您包括的对象而不是指向 Box 的指针。在你的Entity类,它还需要知道 class Box 的大小(需要 Box 类的完整定义)和成员的定义(因为它将访问 Box::Box 以初始化实际字段),因此您需要 Box 的完整定义在将其作为您的 Entity 中的一个字段之前类(class)。

    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;
};

关于c++ - 无法声明其他类的类字段类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18800435/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com