gpt4 book ai didi

c++ - #include 彼此的两个类出错

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

假设我有一个名为 Foo 的类和另一个名为 Bar 的类。 Bar 包含一个 Foo 的实例,我在 Foo 中有一个函数将 Bar 作为参数。但是,当我在 Foo#include "Bar.h" 以允许 Foo 看到 Bar 时,我得到了这个引用 Bar 的行出错:

error: ISO C++ forbids declaration of 'Foo' with no type

我猜这是因为这两个类在编译时相互依赖。有什么办法可以解决这个问题吗?

编辑:这两个类都有头文件,其中另一个类在 #ifndef 声明中被引用。

最佳答案

Foo.h 中,您需要使用前向声明 class Bar; 而不是包含 Bar.h。请注意,要使其正常工作,您需要将参数 Bar 作为引用或 Foo 类中的指针。

关于c++ - #include 彼此的两个类出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9662557/

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