<分区>
我是 C++ 的新手,我的大部分作品都是用 Python 编写的。
在 Python 中,如果我想创建一个类来保存有关人类的信息,我可以编写一个可以将其“父”作为其变量之一保存的类。在 Python 中,我会大致这样做:
class Human:
def __init__(self, name):
self.name = name
first = Human("first")
second = Human("second")
second.parent = first
second.parent = first
表示人类 second
的父级是人类 first
。
在 C++ 中,我尝试实现类似的东西:
class Human {
public:
Human parent;
};
int main() {
Human first = Human();
Human second = Human();
second.parent = first;
}
此示例带有一个错误,即 field has incomplete type: Human
。我明白了,因为它说我的人类对象中不能有人类,因为人类是什么还没有完整的定义。当我搜索相关帖子时,我不断地遇到使用前向声明和指针的解决方案,但我一直无法使其正常工作。
我非常感谢任何帮助使 c++ 示例按照我想要的方式运行。
谢谢。