Archived

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

?

This topic is 5152 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
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
See "Header Files" link in my signature.

And please make your thread titles more meaningful in the future.


[ Start Here ! | How To Ask Smart Questions | Recommended C++ Books | C++ FAQ Lite | Function Ptrs | CppTips Archive ]
[ Header Files | File Format Docs | LNK2001 | C++ STL Doc | STLPort | Free C++ IDE | Boost C++ Lib | MSVC6 Lib Fixes ]

Share this post


Link to post
Share on other sites