Page 1 of 1

#include drives me crazy

Posted: Tue May 31, 2005 8:28 pm
by MeisterK
Sure, this no Irrlich specific question but due to the good experience with the community I post this in the hope of a hint.

Situation:

I've two classes (classA and classB) Both are implemented in a cpp file and both having a header file.

Further more is classA using classB and vice versa. To accomplish this I need to include classA.h in classB.h and vice versa.

But this means to set up an eternal "include-loop" between the two headers.

Consequently my compiler (VC2003) is not very amused about this try.

I've tried a lot (even browsing a book about C++) but without success.
The A-uses-B and B-uses-A situation isn’t so odd, isn’t it?

So my question:

How to deal with such a situation?

Thanks

Thorsten

Posted: Tue May 31, 2005 9:02 pm
by Guest
thats exactly my problem and i dont know how to do it, i guess its not possible so easily.

maybe like this:

class A:
include class B and make an instance of it, then use the data

class B: derrive from class A and use the data



ps: i head its not possible :? this problem and some timing problems with irrlicht are driving me crazy (i just want a simple sprite to move up 1.0f per tick, this damn #+!%$ is not possible outside the while(IrrlichtDevice->run()) mainloop, isnt it?!!! )

its really demotivating and frustrating :?

Posted: Tue May 31, 2005 9:05 pm
by Luke923
Try adding the following to your header files:

Code: Select all

#pragma once

Posted: Tue May 31, 2005 9:37 pm
by hybrid
Try a forward declaration like this

Code: Select all

class A;
class B {
  A* useClassA;
}

class A {}
This way you can easily separate the necessary files.

Posted: Tue May 31, 2005 10:01 pm
by ondrew
I also use the forward declaration. I usually have a small file called classes.h with simple content

#ifndef _CLASSES_H
#define _CLASSES_H

class Object;
class Animation;
class Command;
class Game;

#endif

and it works like charm.

Posted: Wed Jun 01, 2005 8:06 am
by MeisterK2
The forward declaration was the solution :-) (at least in my small model app) I didn't know that this is also possible for classes.

The #define and #pragma once approaches (I've tried them the days before) lead me into a situation, that one of the class was undefined (since the headers was only used once in both cases)

@All: Thanks for the fast replies, I stood on the edge of madness because of this :-)

Thorsten