简体   繁体   中英

c++ forward declaration and incomplete type

Hello I have trouble working with forward declaration. I can't access the forwarded class function, though I need to do so.

Here is my Window.h:

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

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

};

Here is Tab.h:

class Window;  // forward delcaration

class Tab {

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

private:
  Window *m_parent;
};

And lastly, Tab.cpp:

#include "Tab.h"

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

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

The compiler returns me the following error: invalid use of incomplete type 'struct Window'

How can I access the parent's function knowing it already includes Tab.h to create tabs? If I can't, what do you advise me to do?

You need the definition of the Window class in order to call

 m_parent->doSome();

So, include Window.h in Tab.cpp .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM