I have two classes. One is a management class, which stores a bunch of worker classes. The workers are actually a templated class.
#include "worker.h"
class Management {
private:
worker<type1> worker1;
worker<type2> worker2;
...
};
The problem arises due to the fact that the templated classes needs to use Management.
Example:
class Worker{
...
};
#include "Worker.inl"
The Worker inl file:
#include "Management.h" //Circular Dependency!
Worker::Worker(){
//Management is accessed here
Management mgmt1();
mgmt1.doSomething(); //Can't use that forward declaration!
}
...
Normally you would forward declare Management.h in the Worker header file, and call it a day. Sadly, since the class is templated, it is always going to get included.
I guess you can claim that the design is bad, since a templated class shouldn’t be templated if it needs to know this sort of information, but it is what it is, and I have to work with it.
You can also view this question as a microcosm of office life.
If no actual members of
Managementare referred by worker.inl (i.e. only pointers/references toManagement), you could forward declare it:Otherwise your only option may be to move the method implementations into a cpp file, not included in the headers.
Note that
Workercan not be forward declared, since it is contained by value inManagement:thus the compiler needs to know its full definition at this point.
You should also consider putting the two class definitions into the same header file, to express that they are interdependent, thus form a component.