1-
Namespaces can be nested i.e.namespaces inside namespaces. The
global namespace is the outermost namespace that everything belongs to.
1 2 3 4 5 6 7 8 9
|
int var;
namespace A
{
int var;
namespace B
{
int var;
}
}
|
Namespace paths can be relative and absolute similar to how file paths work. To refer to something in the global namespace we can put :: in front. This is sometimes needed to be able to refer to the correct symbol and to avoid ambiguous errors that you can get when using the
using keyword.
In the code above it is possible to refer to all three variables from all three namespaces. These functions all do the same thing:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
|
void func()
{
var = 1;
A::var = 2;
A::B::var = 3;
}
namespace A
{
void func()
{
::var = 1;
var = 2;
B::var = 3;
}
namespace B
{
void func()
{
::var = 1;
::A::var = 2;
var = 3;
}
}
}
|
I don't know why you get the error. I was thinking that maybe you left something out or there is something named
Polygon declared in the global namespace inside one of the headers that you include. The complete error message probably give more details.
2-
The using declaration in my earlier post tells the compiler that it should search for symbols inside the Graph_lib in addition to everywhere that it searched before. You can even have multiple using declarations of many different namespaces. Well of course there could have been a rule about which one the compiler should prefer instead of giving an error but that is likely to lead to bugs that are hard to spot. You often don't know every symbol name inside other namespaces and how they collide with your own so better have an error than using the wrong one.
It is possible to tell the compiler to prefer a symbol inside a certain namespace but you have to do it for each and every symbol. If you replace (or add after) line 14 with
using Graph_lib::Polygon;
in my earlier post you wouldn't get an error.