Jump to content
  • Advertisement

Archived

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

UeberBobo

?

This topic is 5460 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

to get more organized i split up my code so that i have: main.cpp globals.h functions.h i want both main.cpp and functions.h to use the globals.h variables (globals.h stores constant global vars) but i get errors saying that all vars in globals.h are trying to be declared twice sine i have #include "globals.h" in two places. how do i solve this?

Share this post


Link to post
Share on other sites
Advertisement
declare the globals as "extern" in the header, then define it in one source file.

for example:

Source.cpp:

int Code;


Source.h:

extern int Code;

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
At the top of the globals.h file, you need to use some preprocessor directives. You would probably put something like:

#ifndef _GLOBALS_H_
#define _GLOBALS_H_

Then, at the very bottom of the globals.h file, enter the following:

#endif

Now, you can replace _GLOBALS_H_ with anything you want; this is simply a suggestion. However, whatever you change it to, they must be identical on both the #ifndef and the #define lines.

Basically, the first time the compiler (actually, the preprocessor, but we''re trying to make this easy) encounters the #ifndef line, _GLOBALS_H_ will not be defined, and will then proceed to have it defined with the #define line, and include the code between the #define line and the #endif line. Any time the compiler encounters that line (in any file) it will skip all the code until it encounters the matching #endif line.

I would suggest searching for preprocessor directives. There''s several more, and they can be pretty handy.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!