c++ - 使用循环包含时出现“使用未定义类型”错误

标签 c++ compiler-errors circular-dependency forward-declaration

<分区>

我用这些依赖项编写了两个类。

//a.h
class B: 
class A{
 B *b ;
 A() { b = new B(this);}
 print();
}



  // b.h
    class A;
    class B{
     A* a;
     B(A *_a){ this->a = _a }
     void foo() { a->print(); } // here i have error : use of undefined type a
   }

如果我注释 'a->print()' 代码编译并且其他函数工作,但是当我在类 B 中使用类 A 的指针时,我收到此错误:

"use of undefined type a"

谁能解决我的问题?

谢谢

最佳答案

您需要将方法的实现移到 header 之外并移到实现文件中。

要执行 a->print();new B(this),需要完整的定义。

关于c++ - 使用循环包含时出现“使用未定义类型”错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24511501/

相关文章:

c++ - 如何在主函数中连接到数据库,然后从其他函数中引用它

c# - 在 C# 中定义常量

c++ - 使用 Visual Studio 编译 hunspell

java - 在执行需要身份验证的http调用时如何解决循环依赖场景?

c++ - 循环依赖,即使类是前向声明的

python碰撞检测解决循环依赖

c++ - 有 C++11 临界区吗?

c++ - 手动为按钮、工具栏、选项卡等绘制渐变?

python-3.x - IndexError : index 43462904 is out of bounds for size 43462904

groovy - 使用poi.jar作为数据接收器脚本时出现Groovy编译错误