|
-
June 11th, 2011, 01:06 AM
#1
global variable not part of namespace
So the issue is ive declared a variable in one file and called it in another like so:
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
-
June 11th, 2011, 01:11 AM
#2
Re: global variable not part of namespace
Can the mod delete this post im gonna move it to another index.
-
June 11th, 2011, 03:09 AM
#3
Re: global variable not part of namespace
 Originally Posted by Scubastooge
Can the mod delete this post im gonna move it to another index.
What is an "index", and why should the moderator delete the post? You're asking a C++ question, so which forum do you think it belongs in?
Second, I don't see any declaration of "var1" in File2.cpp, so the error is obvious. A C++ source module such as File2.cpp has no idea that you've declared some variable in File1.cpp. A C++ compiler only looks at the definitions in the current moducle it's compiling.
Regards,
Paul McKenzie
-
June 11th, 2011, 03:41 AM
#4
Re: global variable not part of namespace
var1 is global scoped though. shouldn't i be able to access it from any file?
-
June 11th, 2011, 04:30 AM
#5
Re: global variable not part of namespace
Yes but as Paul said it has to be declared so the compiler knows what var1 is. Either you add a header and include that or you just declare manually
-
June 11th, 2011, 09:30 AM
#6
Re: global variable not part of namespace
 Originally Posted by Scubastooge
var1 is global scoped though. shouldn't i be able to access it from any file?
When you compile a source module, the compiler only knows what it is currently in the file being compiled. The compiler knows nothing about what you did in another file.
To let the compiler know that there is a variable somewhere out there called "var1", you must tell the this to the compiler. To do that, you use the extern keyword to inform the compiler that "there is a variable called var1 out there somewhere, not declared in this file, but in another file, so it's OK to use it here".
Regards,
Paul McKenzie
Last edited by Paul McKenzie; June 11th, 2011 at 05:27 PM.
-
June 11th, 2011, 11:42 AM
#7
Re: global variable not part of namespace
Note also that you will need to initialize that pointer before you can use it anywhere, much less in a different file.
-
June 13th, 2011, 10:17 PM
#8
Re: global variable not part of namespace
add "extern classobject *var1;" in file2.cpp will be ok.
using global variables in cpp is a very bad behavior!!
you should use the singleton pattern instead.
-
June 14th, 2011, 04:10 AM
#9
Re: global variable not part of namespace
 Originally Posted by deyili
using global variables in cpp is a very bad behavior!!
you should use the singleton pattern instead.
While I agree that using global variables is often bad design, using singletons as glorified globals is not much better. You should design to remove the need for static or global data, not just replace an evil with a lesser evil.
Cheers, D Drmmr
Please put [code][/code] tags around your code to preserve indentation and make it more readable.
As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky
-
June 14th, 2011, 04:49 AM
#10
Re: global variable not part of namespace
 Originally Posted by D_Drmmr
While I agree that using global variables is often bad design, using singletons as glorified globals is not much better. You should design to remove the need for static or global data, not just replace an evil with a lesser evil.
yes, you are right. but how do we do when global data sharing is inevitable ?
-
June 14th, 2011, 07:38 AM
#11
Re: global variable not part of namespace
 Originally Posted by deyili
yes, you are right. but how do we do when global data sharing is inevitable ?
I can't imagine a situation where global variables are inevitable, but in certain cases they may be the best option. In such cases, a singleton may be a good design, but it is not a "one size fits all" solution.
The point is that merely replacing global variables with singletons does not address the design flaw that mostly underlies the use of globals.
Cheers, D Drmmr
Please put [code][/code] tags around your code to preserve indentation and make it more readable.
As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|