Skip to main content
gamedev.net
🔒 Locked

Problem with Classes

Started by Snash Aug 4, 2002 at 6:38 AM 3 replies 300+ views
Original Post
Snash
Snash
Hi, I''m having trouble making a Class Object Global. I have a CPlayer class which hold my player info and so on. And I have a main.cpp <- holds the main routine. DirectInput.cpp <- Input GameCore.cpp <- The game loop CPlayer.CPP <- Class define CPlayer.H <- Class define The problem is that I can''t make my CPlayer Player; Global so I can use it in every one of my source files. If I initalize CPlayer Player in my main.h file (which is linked to all the other files) I just get a compile error, like it doesnt know what CPlayer but I did include it. Can''t I Init my CPlayer Player; in a header file? It works fine in one of my CPP files, but the problem is that if I create my Player object in one of my CPP files, I can''t use Player else where. How Do I make my Player global? thanks for your time
Glass_Knife
Glass_Knife
Hello,
I''m sure there are many ways to do this, but this is how I do it.
You might be getting a compiler error is the object you want to make global contains Directx stuff, which must be set up before the object is made, but because the object is global, it is created before the game stuff is set up (BAD)
Check out this link, read it, and come back and look at my code:
Singleton Design Pattern


  

class CSomeClass{
public:
static CSomeClass* Instance();
private:
//note the constructor is private!!!

CSomeClass();

};

CSomeClass* CSomeClas::Instance()
{
static CSomeClass obj;
return &obj
}

//in winmain...


CSomeClass* someclass = CSomeClass::Instance();



by making an object a Singleton, there is only ever one, and you can put it in all your classes. I have a CDirectDraw class that goes in any other class that needs to use it. It works out very well.
Hope this helps

I think, therfore I am.
I think?
Snash
Snash
My class does not have any Direct Stuff. It''s only some Vars: like float X,Y,Z .. Bool isdead and some functions to make change the XYZ, nothing releated to DirectX.

Snash
Snash
Yeah I know, but the problem was that I wrote

extern CPlayer Player;

That gave me an error. But

extern class CPlayer Player;

worked fine. Problem solved


Kylotan: Good article by the way

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.