Jump to content
  • Advertisement

Archived

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

Neo Genesis10

Out of scope variables

This topic is 5157 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

A weird logical error cropped up recently and I have only recently been able to figure out its cause. I have a main header file which includes some global declarations and the other header files I use. This header file is called in several different source files because I need to use the declarations inside it. Here''s my problem. It appears that my variables are being created multiple times. For example, I have a ''console'' structure instance which stores general information, such as debug data. When, however, control moves away from the source file which inputted the data the data resets to blank and only returns when control is back with the original source. This, however, also seems to be happening with my class instances as well. Note that they all share the same variable name because I believe the code in the header file is being excecuted multiple times. This is despite the fact I enclosed the code in #ifndef / #endif commands. I am using the ''static'' keyword to declare my variables. But if I remove the keyword, I get hundreds (literally) of link errors where it tries to redeclare the variables. Has anyone encountered this problem (using VC++ 5) and, more importantly, does anyone know how to fix it? Worship the holy trinity that is Blitz, Babes and Beers!

Share this post


Link to post
Share on other sites
Advertisement
This header file is called in several different source files because I need to use the declarations inside it.

Odds are you have placed variable definitions in your header, not variable declarations.

int i; ← This is a definition.
extern int i; ← This is a declaration.

It appears that my variables are being created multiple times.

A variable can be declared multiple times, in different translations units (a ''cpp'' file+all the headers it includes), but must be defined exactly once across all your translation units.

This is despite the fact I enclosed the code in #ifndef / #endif commands.

This only protects from multiple inclusion within a single translation unit. Each cpp file is compiled independently.

I am using the ''static'' keyword to declare my variables.

The static keyword means the variable is private to the translation unit (note - this behaviour is deprecated in C++, use anonymous namespaces instead). That is, each translation unit gets an individual copy of the variable. The static int i; used by ''foo.cpp'' is not the same as the one used by ''bar.cpp'', even though they both included the same header. Needless to say, this is probably not what you want to do here.

Solution:

Copy the variables definitions into one ''cpp'' file and add the extern keyword where appropriate in the header files.

Also, read this article thoroughly.


“Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.”
— Brian W. Kernighan

Share this post


Link to post
Share on other sites
Hit the nail squarely on the head. Solved my problem within minutes of reading your post. Thanks a heap!

Worship the holy trinity that is Blitz, Babes and Beers!

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.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!