C++, global class instance

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
X_for_Extra
Posts: 69
Joined: Mon May 23, 2005 4:42 pm

C++, global class instance

Post by X_for_Extra »

Yo!
I've made a class that I need to access from all over the place.
So, in my main.cpp I have

#<include> "theclass.h"
TheClass * TheInstance;

..int main()..
TheClass * TheInstance = new TheClass;

and finally..
delete(theInstance);

This compiles and runs fine but does not make the instance accessable from the other files. So I put
extern TheClass * TheInstance;

in my globals header that gets included everywhere. The global header also includes the declaration of the class so it shoud be visible.

Here I get errors though:

expected init-declarator before '*' token
expected `,' or `;' before '*' token

...pointing to the line in the global header.

HELP?
Should put something witty here I suppose.
jox
Bug Slayer
Posts: 726
Joined: Thu Apr 22, 2004 6:55 pm
Location: Germany

Post by jox »

Put the "extern TheClass * TheInstance" in each cpp file where you need it. I think it's not valid to put extern declarations in header files.
It is like it is. And because it is like it is, things are like they are.
mepeisen
Posts: 2
Joined: Fri May 27, 2005 5:17 pm

Post by mepeisen »

its valid.


global.h

class TheClass; // forward declaration

extern TheClass* TheInstance;



main.cpp

TheClass* TheInstance;

int main()...

TheClass* TheInstance = new TheClass();
//
delete TheInstance;


something.cpp

#include "global.h"
#include "TheClass.h" // You need a class description when accessing methods or fields

...
TheInstance->doSomething();
X_for_Extra
Posts: 69
Joined: Mon May 23, 2005 4:42 pm

Post by X_for_Extra »

So I need a Forward declaration... its so that the compiler has an idea about what a name means even though it hasnt got around to that file yet.. right?

Makes sense. Ill try it when I get home. Thanks fellas!
Should put something witty here I suppose.
X_for_Extra
Posts: 69
Joined: Mon May 23, 2005 4:42 pm

Post by X_for_Extra »

YAY It works!
Should put something witty here I suppose.
Post Reply