global variable not part of namespace

Jun 11, 2011 at 5:49am
So the issue is ive declared a variable in one file and called it in another like so:

1
2
3
4
5
File1.cpp:
classobject *var1;

File2.cpp
::var1->Dofunction();


This returns:


'var1' : is not a member of '`global namespace''


I'm still a little new to this so it might just be the syntax here.

Thanks in advance
Jun 11, 2011 at 6:10am
1
2
3
4
5
ile1.cpp:
classobject *var1;

File2.cpp
var1->Dofunction();
Jun 11, 2011 at 6:16am
If i do that i get the output

'var1' : undeclared identifier
Jun 11, 2011 at 9:56am
You need to declare the variable in File2.cpp as an external. I suggest putting it in a header file and including that.

The reason for both of your errors, as far as I can see, is that the compiler can't find the variable. Separate source files (translation units) are compiled separately, so they can't share variables by default.

This declares an external integer variable called i
extern int i;
so the compiler accepts that it is then defined in another translation unit, and leaves it for the linker to put it all together.
Topic archived. No new replies allowed.