0

CustomManager is a Manager, and A.hpp uses CustomManager, and Manager uses Data from A.hpp, hence the includes.

My understanding with forward declarations is to disallow the circular dependency by not including the whole file and rather just providing a reference so it recognizes it rather than complaining about it.

However, commenting out CustomManager.hpp from A.hpp errors out in A.cpp despite the forward declaration for CustomManager. Why is that so?

cannot convert CustomManager to Manager&

// ------ A.hpp ------ 

// #include "CustomManager.hpp"   // uncommenting this out builds fine!

// forward declarations
class Manager;
class CustomManager;

using namespace values
{
enum class Data : int
{
  FIRST = 0,
  SECOND
};

class A
{
   std::unique_ptr<CustomManager> customMgrPtr;
   public:
   void execute(Manager& mgr);
   void run();
};

// ------ A.cpp ------
#include "A.hpp" 
void A::run()
{
  execute(*customMgrPtr);    // ERROR: cannot convert CustomManager to Manager&
}
}

// ------ Manager.hpp ------ 
#include "A.hpp"
using namespace values
{
enum class Data : int;  // forward declaration from A.hpp

class Manager
{
};

// ------ CustomManager.hpp ------ 
#include "Manager.hpp"
class CustomManager : public Manager
{
};
}
xyf
  • 664
  • 1
  • 6
  • 16

0 Answers0