The short answer is that you should add
class Cars;
in dealers.h before the definition of class Dealers, and you should add
class Dealers;
in Cars.h before the definition of class Cars. This tells the compiler that these classes exist and will be declared later.
Now the long answer. I suspect that Cars.h looks something like this:
1 2 3 4 5 6 7 8 9
|
#ifndef CARS_H
#define CARS_H
...
#include "Dealers.h"
...
class Cars {
...
};
#endif
|
Now consider what happens if your main program has
#include "Cars.h"
:
- The pre-processor includes Cars.h. CARS_H isn't defined at line 1 so it defines it at line 2.
- At line 4, Cars.h includes Dealers.h.
- Dealers.h includes Cars.h. But this time
CARS_H is defined, so the pre-processor skips over the bulk of Cars.h.
- The preprocessor continues with Dealer.h. It defines class Dealers and then sees the reference to the
undefined class Cars.
- When the preprocessor finishes with Dealers.h, it returns to line 5 of Cars.h.
- Now it sees the definition of class Cars.