【问题标题】:c++ forward declaration and incomplete typec++前向声明和不完整类型
【发布时间】:2014-01-15 09:28:45
【问题描述】:

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

这是我的 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 来创建标签,我如何才能访问它? 如果我不能,你建议我怎么做?

【问题讨论】:

  • #include Tab.h 那是你的实际代码吗?
  • 你的意思是我打错字了吧?应该是#include "Tab.h"

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


【解决方案1】:

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

 m_parent->doSome();

所以,在Tab.cpp 中包含Window.h

【讨论】:

  • 哇它好用!根据新手教程,我认为禁止在 .cpp 文件中包含标头
  • @AlexandreToqué 好吧,您已经在 .cpp 文件中包含了标题(例如,Tab.hTab.cpp 中。您可能误解了本教程。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-06-18
  • 1970-01-01
  • 2014-02-02
  • 2016-11-06
  • 2011-10-22
  • 2013-07-09
相关资源
最近更新 更多