0

I have two classes named Manager and Worker. Each Worker has a Manager and each Manager has a Worker to manage.
I am defining the classes at separately header files.

manager.h

#include "worker.h"

class Manager{
    private:
        Worker* worker;
    public:
        /* methods etc */
};

worker.h

#include "manager.h"

class Worker{
    private:
        Manager* manager;
    public:
        /* methods etc */
};

main.cpp

#include "worker.h"
#include "manager.h"

int main(void){
    /* code code */
}

Ok until now it seems to be everything ok, but when I try to compile it gives lots of lines with the names of the headers. First I thought that it created a kind of loop and was including the headers endless. I then searched about it and tried to use #ifndef #define #endif in each header file. Turns out that now the problem is that the class Worker doesn't see the class Manager and vice-versa, meaning that the errors now are "'Worker' does not name a type" etc etc etc.

Vincent Savard
  • 34,979
  • 10
  • 68
  • 73
Luiz Agner
  • 77
  • 10
  • Look to the right. There are a number of questions that would give you the answer. – Lightness Races in Orbit Feb 29 '16 at 15:38
  • you have to declare one of them first, lets say `Worker`, but then at this point, the `Manager` is not declared yet. As the worker only needs a pointer to a manager (ie. he doesnt need to know the size of a `Manager` object) it is sufficient to supply a forward declaration at the top of the `Worker` header. – 463035818_is_not_an_ai Feb 29 '16 at 15:42
  • 1
    Also: [Resolve circular dependencies in C++](http://stackoverflow.com/questions/625799/resolve-circular-dependencies-in-c) – NathanOliver Feb 29 '16 at 15:43
  • Change `#include "worker.h"` to `class Worker;`. Ditto for manager – Ed Heal Feb 29 '16 at 15:58

0 Answers0