Question

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?

Was it helpful?

Solution

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

 m_parent->doSome();

So, include Window.h in Tab.cpp.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top