The problem is quite simple: you cannot have dependency cycles.
// MainScheduler.h
#ifndef MAINSCHEDULER
#define MAINSCHEDULER
#include "Job.h"
class MainScheduler { friend class Job; };
#endif
// Job.h
#ifndef JOB
#define JOB
#include "MainScheduler.h"
class Job { friend class MainScheduler; };
#endif
What happens here when parsing MainScheduler.h is the following:
MAINSCHEDULER
is not defined, thus the parsing starts
- the preprocessor defines
MAINSCHEDULER
- the preprocessor includes Job.h
- because
MAINSCHEDULER
is already defined, it skips the inclusion of MainScheduler.h
- it includes the tokens from
Job
- includes of Job.h ends
- it includes the tokens from
MainScheduler
This yields the following preprocessor output, which the compiler sees:
// ignored #include "MainScheduler.h"
class Job { friend class MainScheduler; };
class MainScheduler { friend class Job; };
this is why before you introduced the forward declaration the compiler complained about the unknown MainScheduler
symbol in the definition of Job
.
Your headers cannot include themselves in a cycle, and you cannot befriend a member function with only a forward declaration.
I propose you rewrite Job.h as:
class MainScheduler; // forward declaration
class Job {
friend class MainScheduler;
public:
// whatever
};
By befriending the whole class you get away with just a forward declaration and break the cycle.