3

I read Programming Principles and Practice using C++, Stroustrup's book. In chapter 12 and in page 441 there is this code:

//
// This is example code from Chapter 12.3 "A first example" of
// "Programming -- Principles and Practice Using C++" by Bjarne Stroustrup
//

#include "Simple_window.h"    // get access to our window library
#include "Graph.h"            // get access to our graphics library facilities

//------------------------------------------------------------------------------

int main()
{
    using namespace Graph_lib;   // our graphics facilities are in Graph_lib

    Point tl(100,100);           // to become top left  corner of window

    Simple_window win(tl,600,400,"Canvas");    // make a simple window

    Polygon poly;                // make a shape (a polygon)

    poly.add(Point(300,200));    // add a point
    poly.add(Point(350,100));    // add another point
    poly.add(Point(400,200));    // add a third point 

    poly.set_color(Color::red);  // adjust properties of poly

    win.attach (poly);           // connect poly to the window

    win.wait_for_button();       // give control to the display engine
}

//------------------------------------------------------------------------------

When I ran the code I get 13 errors which must of them are about the Polygon identifier. For example first error is: Error C2872: 'Polygon' : ambiguous symbol

Why my compiler doesn't know that Polygon please?

abbasi
  • 121
  • 1
  • 10
  • 1
    Where is Polygon defined? I do not have this book. – drescherjm Jan 07 '14 at 13:59
  • Do you have the header (`.h`) files? – Peter Bloomfield Jan 07 '14 at 14:03
  • 2
    Probably, there's a type by that name in both the global namespace and `namespace Graph_lib`, and the using directive causes the ambiguity. If that's the case, use the qualified name `::Polygon` or `Graph_lib::Polygon`, depending on which you want. – Mike Seymour Jan 07 '14 at 14:09
  • @Peter: I have all the header files ("Simple_window.h" & "Graph.h") mentioned in the code in my include directory. – abbasi Jan 07 '14 at 14:56
  • @Mike: I used _Graph_lib::Polygon_ but got 11 different errors. First error is that: Error LNK2001: unresolved external symbol "protected: virtual void __thiscall Graph_lib::Window::draw(void)" (?draw@Window@Graph_lib@@MAEXXZ) C:\Users\CS\documents\visual studio 2012\Projects\testv\testv\testv.obj – abbasi Jan 07 '14 at 15:03
  • 1
    @abbasi: That means that either you haven't implemented `Graph_lib::Window::draw()`, or you're not linking with the file or library that contains the implementation. – Mike Seymour Jan 07 '14 at 15:04
  • OK, what can I do now? – abbasi Jan 07 '14 at 15:21
  • Look into what Mike said. Is Graph_lib::Window::draw() implemented? If it is are you linking with a library that contains Graph_lib or is the c++ file that implements Graph_lib::Window::draw included in your project. – drescherjm Jan 07 '14 at 18:09
  • OK, but how to implement the _Graph_lib::Window::draw()_? Or how to linking that library? I have added that library into include directory. – abbasi Jan 07 '14 at 19:07
  • No answer for this problem!? – abbasi Jan 08 '14 at 07:40
  • It's extreamly hard to help without the book or at least all the code that is included. – drescherjm Jan 08 '14 at 15:10
  • Also the original problem is now answered correctly by Vlad from Moscow. – drescherjm Jan 08 '14 at 15:17
  • Okay. I found the code from the book that you are taking about. Anyways you have to make sure Window.cpp is in the project that you are building to get the implementation of Graph_lib::Window::draw(). Also you must link with the fltk library that is provided in the sources. – drescherjm Jan 08 '14 at 19:05
  • Adding Graph_lib:: before Polygon worked for me--thanks @MikeSeymour! – KBurchfiel May 08 '20 at 01:12

2 Answers2

3

If the symbol is ambiguous then try to use its qualified name:

Graph_lib::Polygon poly;
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • I used this snip code _Graph_lib::Polygon poly;_ instead of this _Polygon poly;_ and run the code again. Again there were 11 errors, first is this: Error 9 error LNK2001: unresolved external symbol "protected: virtual void __thiscall Graph_lib::Window::draw(void)" (?draw@Window@Graph_lib@@MAEXXZ) C:\Users\CS\documents\visual studio 2012\Projects\Win32Project1\Win32Project1\Win32Project1.obj – abbasi Jan 11 '14 at 09:11
  • It seems that the linker does not see the library Graph where there are definitions of functions. You must be sure that you specified paths to libraries correctly in your project. – Vlad from Moscow Jan 11 '14 at 11:59
  • What every person does with header files is to add them into the _include_ directory and after that, the compiler (here Visual Studio) reads them. And that is exactly what I did, that is, I have added the _Graph.h_ header into the _include_ directory. And I don't know another way of giving the compiler the paths of header files. The thing I have now in my mind is to create a header file of _Graph.h_'s code and replace it with previous _graph.h_ header file in _include_ directory. Agree? – abbasi Jan 11 '14 at 12:51
  • I think that apart from headers there shpuld be some library files that contain definitions. You should say the linker where to search them. – Vlad from Moscow Jan 11 '14 at 12:55
  • I don't know how to do that. And this is a very global answer. Anyway, thank you for your responses. – abbasi Jan 11 '14 at 13:34
  • You need read the instructions how to instal FLTK gor a given compiler. – Vlad from Moscow Jan 11 '14 at 13:46
  • Another question; I want to read the _Graph_lib_ but don't know how. I think I should read both of graph.h and that. Reading graph.h is easy but about the _Graph_lib_ how? – abbasi Jan 11 '14 at 13:49
  • You should correctly install FLTK using instructions or the description presented at the site of Straustrup. – Vlad from Moscow Jan 11 '14 at 13:54
  • There isn't any instruction about installing FLTK on his website except for what is written in his book which is printed in 2008. – abbasi Jan 11 '14 at 14:04
  • I think you can find them at the site from where FLTK is downloaded. – Vlad from Moscow Jan 11 '14 at 14:06
  • The only issue about installing is that I have downloaded and used _fltk-1.3.2-source_ but in his site there is the _fltk- 1.1.9-source_ version. I should better to install the 1.1.9 instead of 1.3.2? – abbasi Jan 11 '14 at 14:15
  • I think it is better to install the last version of FLTK. Only you should investigate how to yse it. – Vlad from Moscow Jan 11 '14 at 14:16
  • OK I search the web on how to correctly install the 1.3.2 version of fltk on Visual studio 2012. But I don't think I installed it incorrectly. – abbasi Jan 11 '14 at 14:21
0

Environment: O/S: Win 10 Pro 1909, VS 2019 v16.4.5

I'm working my way through the second edition of Stroupstrup's PPP book and I am also getting the error message "Polygon is ambiguous".

I think the problem has to do with the Path. When I open a x64 Native Tools Command Prompt for VS 2019 and type in Path, I find a reference to ..\Program Files (x86)\Windows Kits\10\Windows Performance Toolkit\. When I perform a Find in Files in Visual Studio and search for the word Polygon in the Visual C++ Include Directories, I get back 35 results. One of the results is for a structured variable named Polygon in the Windows.UI.Xaml.Shapes.0.h header file. This header file is in the same directory as the aforementioned Windows Kits directory.

Assuming that Visual Studio loads in the same Path as the x64 Native Tools Command Prompt for VS 2019, it appears that Visual Studio is picking up multiple definitions of Polygon. Hence, the necessity to resolve the ambiguity by specifying Graph_lib as the namespace.

Specifying the namespace resolved the error "Polygon is ambiguous" for me. Unlike abbasi, I did not download the FLTK library from the FLTK website. Instead, I used vcpkg to download and install it. For more info on installing FLTK see my answer to this Stack Overflow question.

Allan Head
  • 41
  • 5