C++前向声明和不完整类型

标签 c++ circular-dependency forward-declaration incomplete-type

您好,我在处理前向声明时遇到了问题。我无法访问转发的类函数,尽管我需要这样做。

这是我的 Window.h:

#include "Tab.h"  // Needed because Window will create new Tabs

class Window {
  public:
    ...
    void doSome();

};

这是 Tab.h:

class Window;  // forward delcaration

class Tab {

public:
  class Tab(Window *parent);
  void callParentFunction();

private:
  Window *m_parent;
};

最后,Tab.cpp:

#include "Tab.h"

Tab::Tab(Window *parent) {
  m_parent = parent;
}

Tab::callParentFunction() {
  m_parent->doSome();  // Error
}

编译器返回以下错误: 不完整类型“struct Window”的无效使用

在知道父函数已经包含用于创建选项卡的 Tab.h 的情况下,如何访问父函数? 如果我不能,你建议我做什么?

最佳答案

您需要Window 类的定义才能调用

 m_parent->doSome();

因此,在 Tab.cpp 中包含 Window.h

关于C++前向声明和不完整类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21133515/

相关文章:

c++ - .dll 与带有导出的 .dll,有什么区别?

Angular APP_INITIALIZER 运行时循环依赖

c - 在 C 标准库中转发声明实体?

c++ - 将为数组分配多少内存

java - C++ 和 Java 之间的低延迟 IPC

C++/Qt : track filesystem changes

php - 如何摆脱这种循环依赖?

delphi - 两个类有两个循环引用

c - 不理解我得到的这个前向声明(forward declaration)

c++ - 前向声明与析构函数的关系