#include & circularity

Hey. Here is the situation.

File ControlCentre.h:

1
2
3
4
5
6
7
#include "Receiver.h"
...
class ControlCentre{
   ...
   vector<Receiver> recs; ****ERROR Receiver not declared in this scope****
   ...
}


File Receiver.h

1
2
3
4
5
6
7
#include "ControlCentre.h"
...
class Receiver{
   ...
   Receiver(...., ControlCentre* cc);
   ...
}


Ok. So as you can see, I'm trying to make it so the receiver can communicate with the control centre (its parent). However I seem to be having some trouble with the include statements. (Note: see the error line above).

1. Is this how you could go about allowing 2 way communication between classes (children and parents). And,

2. If so, how can I fix this?

Thanks for any help,

Nick.


See:
http://www.cplusplus.com/forum/articles/10627/

Should have everything you need.
Receiver doesn't actually need the implementation of ControlCenter because it only ever points to it, whereas ControlCenter actually needs Receiver's implementation because it uses it as a value-type.

The solution would be to only use a forwards declaration of ControlCenter in Receiver.h rather than including the whole header, like so:

1
2
3
4
class ControlCenter;

class Receiver {
...


In general you should avoid including a header file for a class if you only need to use that class as a reference type, not a value type.
Last edited on
Topic archived. No new replies allowed.