Skip to main content
gamedev.net
🔒 Locked

namespace usage?

Started by aphelix Mar 28, 2007 at 1:21 AM 7 replies 1.8k views
Original Post
aphelix
aphelix
Hi! (1) how can i use namespaces in another file? when i declare namespace in 1.cpp i can't use it in another file (2.cpp) ex: (in 1.cpp) namespace Global{ bool a = true; }; (2) What is the best solution for global variables? Struct or namespace thanks!
DaBono
DaBono
Quote:
Original post by aphelix
how can i use namespaces in another file?

You can use them by using this form: Global::a = false;
Quote:
Original post by aphelix
What is the best solution for global variables? Struct or namespace

Well, the 'best' solution would be to use global variables as few as you can, and then constrain them to one cpp-file.
If you really need to expose them, I prefer a namespace. Structures imply to me that the variables belong together, whereas a namespace called globals does not. But that may be stricly personal.
aphelix
aphelix

But,i get
error C2653: 'Global' : is not a class or namespace name
error C2065: 'a' : undeclared identifier
compiler error!

thanks!
wasd
wasd
You need to declare the variable in a header file and include it in 2.cpp:

Globals.h:
namespace Global{bool a;};
arthur83
arthur83
Quote:
Original post by Sc4Freak
Won't that give a redefinition error?


Nah, you can declare variables, types and functions in namespaces wherever you feel like, just like in C# or Java.

1.h:
namespace Global{   extern bool a;}


1.cpp:
namespace Global{   bool a;   void foo()   {      a = false;   }}


2.cpp:
#include "1.h"namespace NotSoGlobal{   void bar()   {      Global::foo(); // Global::a == false   }}
jpetrie
jpetrie
Quote:

how can i use namespaces in another file?


Namespaces, like nearly everything else, must be seen by the compiler before they can be used. Just because foo.cpp has a namespace called Foo in it doesn't mean that bar.cpp can use that namespace. Since each translation unit in C++ is compiled in complete isolation, bar.cpp must #include some header that defines the Foo namespace before it will be able to use it. Typically this means that foo.h will define the namespace and declare its contents, and bar.cpp will #include "foo.h" -- furthermore, bar.cpp will only be able to use what is declared in foo.h, it will not have access to all of the Foo namespace. Consider the following:

// foo.hnamespace Foo{  void FooFunction();}// foo.cpp#include "foo.h"namespace Foo{  void FooFunction()  { ... }  void FooFunction2() { ... }}// bar.cpp#include "foo.h"int main(){  // okay, declared in foo.h  Foo::FooFunction();   // not okay, not declared or defined in bar.cpp's translation unit.  Foo::FooFunction2(); }


Another option is to use "using directives" (of the form "using namespace Foo;"), but these can be dangerous is misused (particularly if used in header files, because they defeat the whole purpose of namespaces by introducing everything in the namespace into the global namespace).

Quote:

But,i get
error C2653: 'Global' : is not a class or namespace name
error C2065: 'a' : undeclared identifier
compiler error!

See my example, above. The namespace Global has not been seen by the compiler at the point you're trying to use it, nor has the variable a. The most direct solution is a crude one: place "extern int a;" into the Global namespace in a header file, and include that in the source file you're trying to use Global::a in (like in arthur83's example -- don't forget to also declare 'a' without the extern someplace to actually provide the location of 'a'). This is a very crude, unfortunate solution, but it will compile.

Quote:

What is the best solution for global variables? Struct or namespace

The best solution is to avoid globals unless you absolutely cannot. Once you cross that threshold, the best solution depends on what exactly you're doing -- it's problem specific. The optimal solution may involve using a struct to aggregate a bunch of variables, a namespace to isolate them from the name-collisions, both a struct and a namespace, or neither.

Quote:

Nah, you can declare variables, types and functions in namespaces wherever you feel like, just like in C# or Java.

The example you have given is different than that given by f8k8, which is incorrect and will cause redefinition errors. Your example works because you've used the "extern" keyword. C++ operates much differently from C# (at least, I don't know enough about Java) in this regard.
Bregma
Bregma
Quote:
Original post by Sc4Freak
Won't that give a redefinition error?


Remember that names at namespace level have external linkage by default.
Quote:
Original post by aphelix
What is the best solution for global variables? Struct or namespace

All variables in C++ are in a namespace. So-called global variables are just namespace-level variables in a specially-named namespace: that namespace can be explicitly used to qualify a name (eg. ::myGlobalVariable or ::fopen()). That namespace has no special features or attributes and is treated just like any other namespace when it comes to linkage rules, lookup rules, and resolution rules.

So the answer to this questions is, your namespace-level names are already in a namespace. Choose the name of that namespace wisely.

--smw
Stephen M. Webb
Professional Free Software Developer

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.