#include drives me crazy

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
Post Reply
MeisterK
Posts: 66
Joined: Sat Feb 26, 2005 1:20 pm

#include drives me crazy

Post 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
Guest

Post 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 :?
Luke923
Posts: 59
Joined: Wed Nov 05, 2003 5:26 am

Post by Luke923 »

Try adding the following to your header files:

Code: Select all

#pragma once
"Object-oriented programming is an exceptionally bad idea which could only have originated in California."
- E.W. Dijkstra
hybrid

Post 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.
ondrew
Posts: 20
Joined: Fri Oct 03, 2003 2:24 pm
Location: Czech Republic

Post 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.
MeisterK2

Post 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
Post Reply