1

I after vast effort could (details are in here FLTK version 1.3.2, Visual studio 2012 and the first example of Stroustrup's PPP book) to ran the below code;

#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::?

Community
  • 1
  • 1
abbasi
  • 121
  • 1
  • 10

2 Answers2

5

Most likely you have something else called Polygon. Namespaces exist so that you can avoid this type of problem.

Here's a simplified example:

namespace Foo
{
  struct Bar {};
}

void Bar() {}

int main()
{
  using namespace Foo; // I really really know what I am doing, seriously!
  Bar b;               // Oh dear.
}

In the output:

error: reference to 'Bar' is ambiguous

This is one good example of the pitfalls of the using namespace X anti-idiom. This really should be avoided, or at the very least limited to the smallest of scopes.

juanchopanza
  • 223,364
  • 34
  • 402
  • 480
  • If you would be so kind to mention explicitly that "so this is why should not be (ab)using namespace directives"... –  Jan 19 '14 at 13:03
  • 1
    @H2CO3 Done (avoiding my usual "never say `using namespace X`" rant which invariably leads to a stream of comments). – juanchopanza Jan 19 '14 at 13:07
  • 1
    Thanks! (Of course it leads to a comment fight -- not everyone can be right nor experienced. If someone isn't, he will find it a personal offense if you tell him never to `use namespace X`... :/ ) –  Jan 19 '14 at 13:08
  • So, _using namespace X;_ and then _Polygon poly;_ with _X::Polygon poly;_ do not equal to each other, yes? – abbasi Jan 19 '14 at 15:14
  • @abbasi Right. If there is something else called `Polygon` in the same scope the first version results in ambiguity, the second one doesn't. – juanchopanza Jan 19 '14 at 15:17
  • Excuse me if I extend the discussion but my intention is to completely understand the issue. You said _in the same scope_, what scope (in this code) are you referring to? The scope of the _main_ function? Or the scope of the _Graph_lib_? – abbasi Jan 20 '14 at 06:26
  • @abbasi in this particular case, the scope is the `main` function. That is where `Bar b` is declared. But it could apply to some other function, the global namespace, a class, etc. – juanchopanza Jan 20 '14 at 06:29
0

Likely, there is another Polygon declared either in global namespace, or in some namespace you're already "using" (like if you have using namespace XXX; in another included header, which is not a good practice, and something like XXX::Polygon is declared).

What I can suggest you as a quick solution (if you really want to use Graph_lib's Polygon without "Graph_lib::", is

using Graph_lib::Polygon;

instead of (or next to)

using namespace Graph_lib;
kikobyte
  • 334
  • 1
  • 10