Connecting Tech Pros Worldwide Forums | Help | Site Map

Global variables in include file

razael1@gmail.com
Guest
 
Posts: n/a
#1: Dec 7 '06
Hi,

I am declaring a global variable in a .h file like this:

extern MyClass object;

Then I include the .h file in other files and try to use object. I get
"object not declared", but then when I add:

MyClass object;

to the file I am including the .h file in, I get "multiple definitions
of object". How can I fix this?

Thanks,
Colin


Joseph Paterson
Guest
 
Posts: n/a
#2: Dec 7 '06

re: Global variables in include file


You shouldn't define global variables in header files, you should only
declare them there.
For example, in myFile.cpp you would have:

int myGlobal = 1;

and in myFile.h you would have:

extern int myGlobal;

now all files that include myFile.h will have access to the global
variable myGlobal.

Joseph.

Closed Thread