PDA

View Full Version : Headers Mutually Inclusive



squeegedog
1st March 2014, 05:32
I know in general it is a bad practice to have two class declarations that are dependent upon each other, but it seems to be the only way to achieve what I'm attempting.

I have a class GameScene that is inheriting from QGraphicsScene. I have another class Troop, that is inheriting from QGraphicsObject.

GameScene does not have any members that are of type troop, just some signal/slot connections and occurrences where it will be passed an object of type Troop and call its public member functions.

Troop needs to have a pointer to GameScene in order to call a member function from GameScene. Unfortunately, I cannot use the scene() function from the QGraphicsItem library as the function does not exist on a typical QGraphicsScene.

This whole situation is leading to where I cannot include "troop.h" in GameScene and "gamescene.h" in Troop because it creates an infinite loop, and forward declaring either of the classes in the other class will not allow for access to member functions, so is there another work-around to this situation?

Thanks.

ChrisW67
1st March 2014, 06:34
You do not need a complete declaration, i.e. The header, for a class in order to declare a pointer to an object of that class. You can use a simple forward declaration in each header for the class:


class ClassA;

class ClassB {
...
void doStuff(ClassA *classA);
...
}


You will need the complete header for ClassA included in the cpp file that provides the implementation of ClassB.

squeegedog
1st March 2014, 06:56
You will need the complete header for ClassA included in the cpp file that provides the implementation of ClassB.

That's the part I was missing, thanks