Reputation: 121
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::
?
Upvotes: 1
Views: 2334
Reputation: 227578
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.
Upvotes: 5
Reputation: 344
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;
Upvotes: 0