Why namespace doesn't work in that code?

Hi all,

I after vast effort could (details are in here http://stackoverflow.com/questions/21063872/fltk-version-1-3-2-visual-studio-2012-and-the-first-example-of-stroustrups-ppp) to ran the below code;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
  #include <Simple_window.h>    
    #include <Graph.h>  
    
    //*********************************************
    
    int main()
    {
        using namespace Graph_lib; 
    
    	Point tl(100,100);
        Simple_window win(tl,600,400,"Canvas");
    
    	
    	Graph_lib::Polygon poly;
    	poly.add(Point(300,200));
    	poly.add(Point(350,100));
    	poly.add(Point(400,200));
    	win.attach(poly);
    
    	
    	win.set_label("Canvas");
        win.wait_for_button();   
    }


But for Polygon poly; I should use Graph_lib::, while since I have added the statement using namespace Graph_lib; at the top of the code's body (just below the main function) so there should not be any need to use the Graph_lib:: for Polygon. But in effect without using it I get ambiguous symbol error. and also even I remove that statement (using namespace Graph_lib;) I don't get any error. My question is that why that statement doesn't work for that code and I have to use Graph_lib::?




Have you declared something else called Polygon? Ambiguous symbol error means the compiler found two or more meanings for that symbol so it doesn't know which one you are referring to.
All of the code is that. The problem is that why when I use Graph_lib::Polygon poly; there is no error but when I use using namespace Graph_lib; and then Polygon poly;, I get that error? Don't those two equal to each other?
I think your problem is that you have a Polygon class defined in the global namespace.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
namespace Graph_lib
{
	class Polygon {};
}

class Polygon {};

int main()
{
	::Polygon poly1; // OK
	Graph_lib::Polygon poly2; // OK
	Polygon poly3; // OK, same as ::Polygon

	using namespace Graph_lib;
	
	Polygon poly4; // Ambiguous! Do you mean Graph_lib::Polygon or ::Polygon?
}


If that's not it, can you please post the exact error message you get?
Last edited on
Thank you Peter.
I don't want get your time much. So, if possible, please answer these questions then I think every thing will be clear for me!

1- What do you mean by Global namespace? As you see my code in my first post, I haven't declared any class named Polygon to leads to ambiguous error.

2- In your code, in line 14 you have specified to the compiler that it should uses the scope of the Graph_lib from now on. But why the compiler again confuses to use the Polygon of what scope?
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.
Last edited on
OK dear Peter.
Your answers are very informative, thank you.

But about the errors, First is; The problem is solved and I don't want get you into the depth of the past problem.

But if you like, you can read the entire and detailed version of the problem here: http://stackoverflow.com/questions/21063872/fltk-version-1-3-2-visual-studio-2012-and-the-first-example-of-stroustrups-ppp
Topic archived. No new replies allowed.