Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Global variables in include file

4 views
Skip to first unread message

raz...@gmail.com

unread,
Dec 6, 2006, 9:38:51 PM12/6/06
to
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

unread,
Dec 6, 2006, 9:44:48 PM12/6/06
to
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.

0 new messages