Share global variables between classes
I had a global variable that could be seen by all of my functions. I was getting too many functions in the same file, so I made functions.h and functions.cpp. I put the function definitions in functions.cpp and the declarations in functions.h. In main.cpp, I include functions.h. The problem is, now I can't see the global variables in the functions that I have moved! Is there a way to see them still?
I thought I might just have to move the global variable definition to BEFORE the include statements, but that didn't change anything, I still get "undeclared identifier".
Thanks,
Dave
daviddoria
Posting Virtuoso
1,996 posts since Feb 2008
Reputation Points: 437
Solved Threads: 204
sure I could use a define, but I need to determine the value of this variable at the beginning of the program, so I can't #define it! So can you just not see this type of global variable inside another .cpp file?
daviddoria
Posting Virtuoso
1,996 posts since Feb 2008
Reputation Points: 437
Solved Threads: 204
great link! I haven't tried it yet, but it seems like I have to "re" define the variable using the "extern" command:
void FuncThatCantSeeTheVariable()
{
extern int TheVariable;
cout << TheVariable;
}
I'll report back with results.
Thanks!
Dave
daviddoria
Posting Virtuoso
1,996 posts since Feb 2008
Reputation Points: 437
Solved Threads: 204