[Solved] Why isn’t C++ inheritance in multiple header and source files working?


You have a cycle in your header inclusion:

  • MainProgram.h includes FileMgr.h
  • FileMgr.h includes MgrBase.h
  • MgrBase.h includes MainProgram.h

You need to break this cycle using forward declarations.

The rule in header files should be: if you only need to declare reference or pointer to a type X, forward declare X instead of including the header which defines it. The same applies if you’re declaring (not defining) a function which has a parameter or return value of type X.

You only have to include the full definition of X if you’re accessing members of X or defining a class derived from X.

In your case:

  • Move both #include statements from MainProgram.h to MainProgram.cpp
  • Remove #include "MainProgram.h" from MgrBase.h

0

solved Why isn’t C++ inheritance in multiple header and source files working?