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?
C++, global class instance
-
- Posts: 69
- Joined: Mon May 23, 2005 4:42 pm
C++, global class instance
Should put something witty here I suppose.
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();
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();
-
- Posts: 69
- Joined: Mon May 23, 2005 4:42 pm
-
- Posts: 69
- Joined: Mon May 23, 2005 4:42 pm