Archived

This topic is now archived and is closed to further replies.

graveyard filla

how do i make an object global to all files?

Recommended Posts

high, my game is 12 files big. anyway, its 5 classes which each have their own .cpp and .h file. plus the main.h and main.cpp which have all the main() functions and stuff. this main file #includes all of the other .h files. anyway, this main file can use any of the other files classes. also, in each of my class .h files will #include main.h. if this is confusing here is a small example: //main. h #include "Entity.h" #include "Audio.h" struct Data { int gameinfohere; } //Entity.h #include "main.h" //Audio.h #include "main.h" anyway, it goes on like this with the 3 other classes i have. anyway, i have a structure called Data inside of main.h (i put it in the above example). how do i make an instance of data and have Audio or Entity use that exact instance? also, how do i let Audio use an instance of an Entity which is declared in main also? for example, in side main.cpp i would do this: Data mydata; Entity player; mydata.stuff = 5; player.xPos = 300; Now, inside of Audio.cpp, i have a function called Blah. inside Blah, i do this: mydata.stuff = 10; <----------- THE COMPILER STARTS YELLING AT ME HERE or player.xPos = 200; <------------ MORE COMPLAINTS i try to use these instances of these classes and Audio has never heard of them! also, if i type Data moredata; OR Entity Another_player; then do moredata.stuff, or Another_player.xPos , it will work! so Audio knows of the classes themselves, but does NOT know of the instance's of the classes.(which is what i need to work with). how do i have them know of each other? this is giving me a real head ahce. i tried to use the extern keyword, but that didnt seem to help much and just gave me errors. prolly cuz i have no clue what the hell im supposed to do. thanks for any help!!! [edited by - graveyard filla on March 29, 2004 2:01:42 AM]

Share this post


Link to post
Share on other sites
This is why the extern keyword was invented. As the compiler works on one file at a time, it won''t know in Audio.cpp what you declared in main.cpp. When you use extern:
extern int foo;
you''re telling the compiler "somewhere in this project, there''s an int called foo. It might not be declared in the file you''re compiling right now, but when you link together the .exe, you''ll find it".
So just put

//main.h

extern Data mydata;
extern Entity player;
//main.cpp

Data mydata;
Entity player;

/K

Share this post


Link to post
Share on other sites
thanks, but just implementing this gives me 97 errors. i added:

extern data data; to the top of main.h. i already had data data; at the top of main.cpp. this gives me 97 errors? i also tried putting extern data data; in different places in main.h. such as right after the includes, before them, and in different spots. this got some errrors down to 18 or so (putting it right after the #includes) but these were about re-definition and missing semi colon. could anyone know whats the problem? i cant finish my game untill i figure this out and i am soooo close. why cant my objects be shared throughout the files! want to know the most screwed up part? if i make a global function, called Blah(). inside this function could mess with any of my objects, and then i can call Blah() from any of my class''es, AND IT WORKS!. ex:

//a global function inside main.cpp and declared in main.h
void blah(int score)
{
data.score = score;
}

//now from inside of Audio.cpp
blah(5); //changes data.score to 5 and it works!
data.score = 5; // ERROR! un-declared ID(or whatever_!!!

am i going to have to write "accessor global functions" so that my classes can use each others objects? i hope not ! thansk for any help !

Share this post


Link to post
Share on other sites
Hi,
The first time I ever came to use extern was on quite a big project. It didn''t work for me. What I recommend you do, is create a smaller project, make sure you fully understand how to use the extern. Then gradually put your main project back together in a new project, adding the externs bit by bit.

=*=
If things seem bad, think that they can get a whole load worse, and they don''t seem so bad anymore

=*=

Share this post


Link to post
Share on other sites
Did you put extern data data; in your audio.cpp? You have to have that before actually use data. It''s also possible to put extern data data; in a header and have that header included in your audio.cpp.





--{You fight like a dairy farmer!}

Share this post


Link to post
Share on other sites
The point is extern needs to come in the file where you actually use the externed variable, if you put it in a header but that header is never included in the file where you want to access the variable then it''s all for nought.

Example of usage:



// === in AppMain.cpp ===


// instantiate the two global variables

int myInt;
float myFloat;

// === in AnotherFile.cpp ===


// promise the compiler they really do exist and we''ve made them elsewhere

extern int myInt;
extern float myFloat;

void AFunc()
{
myInt = 5;
myFloat = 46.042f;
}



Hope that helps.

-Mezz

Share this post


Link to post
Share on other sites
I don''t think your problem is with extern at all. I saw a very bad mistake at the top of your original post which could be causing the problems. In main.h you include Entity.h which includes main.h which includes Entity.h which includes main.h which includes Entity.h which includes main.h which includes Entity.h...are you starting to see the problem? You have a circular include going on. The compiler pastes the code where you have those #includes and each .h includes the other. A way to fix that is to give one of them a forward declaration. In main.h declare your class or structure in one line...


//main.h

struct data;
class Something;
class SomethigElse;


I''m not actually sure if your complier was mad about this error, but it shoud be. Just do a forward declaration of your class/struct, instead of actually including the .h and all should work out.

Dan...

Share this post


Link to post
Share on other sites
thanks guys! Mezz your post saved my life hehe jk but that did fix it.... thanks again everyone! oh, and dan, before i fixed everything, i fixed the circular #includes. basically what i did was take out #include "whatever" out of .h files. and put them in the .cpp files where they were needed. so is it pretty much a given that a header file should NEVER #include another header file? im sure there might be special circumstances, but in general it seems like its not needed ? not sure on that. oh well, thanks again

Share this post


Link to post
Share on other sites
Naaa...it's okay to include .h files in other .h files as long as they both don't include each other. On large projects you just kind of run in to this problem because when one class of your's contains a class object from another class and they DO need to know about each other before the .cpp file you have to do something to fix it. If you ever run into this problem again, just use the forward declaration to fix it as stated in my last post. Good luck...

Dan...

Edit #1:

It's also good to do something like this at the top and bottom of every one of your header files


//Entity.h

#ifndef _ENTITY_H_ //at the top of file

#define _ENTITY_H_


//Whatever goes here


#endif //end the if statement at the end of

//the file



This ensures the .h is only pasted once

[edited by - xdanillumx on March 29, 2004 8:37:43 PM]

Share this post


Link to post
Share on other sites