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
Re: global variable not part of namespace
Can the mod delete this post im gonna move it to another index.
Re: global variable not part of namespace
Quote:
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
Re: global variable not part of namespace
var1 is global scoped though. shouldn't i be able to access it from any file?
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
Re: global variable not part of namespace
Quote:
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
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.
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.
Re: global variable not part of namespace
Quote:
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.
Re: global variable not part of namespace
Quote:
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 ?
Re: global variable not part of namespace
Quote:
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.