Global variable across classes in C++

ElDonAntonio

Senior member
Aug 4, 2001
967
0
0
I may be just completly lost, but I'm unable to declare a global variable that would be used across several classes!

I tried declaring it in a header file that my classes share, but I get linking errors saying the variable has already been defined (even if I included commands like #ifndef TITLE_H #define TITLE_H at the beginning of my header file).
If I declare it static, it will link, but the value of the variable doesn't seem to stay put across my classes (checked that by debugging).

Anyway way to declare a global variable that would be visible everywhere??

thanks guys!
 

singh

Golden Member
Jul 5, 2001
1,449
0
0
Do this:

(1) In any header file, do this: extern int myvariable;
(2) In ANY of the (linkable) source file, define the variable: int myvariable = 10; (this is only done in ONE file)

In any file that you need the variable, be sure to include the file in step 1
 

ElDonAntonio

Senior member
Aug 4, 2001
967
0
0
Hi Singh!

thanks a lot! I wondered how much time it would take for you to come to the rescue...pretty fast, as usual!!! :)

Good to know we have an expert around here!