CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 15 of 15
  1. #1
    Join Date
    Jan 2014
    Posts
    7

    Problem with Polygon in Stroustrup's PPP book

    Hi guys,

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

    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 run the code I get 13 errors which must of them are about the Polygon identifier.
    The problem in that code has to do with Polygon poly; and the rest of the using of it. For example first error is: Error C2872: 'Polygon' : ambiguous symbol

    Why my compiler doesn't know that Polygon please?

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: Problem with Polygon in Stroustrup's PPP book

    And where (in what headers) is Polygon defined? And how?
    Victor Nijegorodov

  3. #3
    Join Date
    Jan 2014
    Posts
    7

    Re: Problem with Polygon in Stroustrup's PPP book

    It is defined in Graph.h header file. And the code of that header is here: http://www.stroustrup.com/Programming/Graphics/Graph.h

    I #included that header into the code but I don't know why the compiler still gives me errors.

  4. #4
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: Problem with Polygon in Stroustrup's PPP book

    Could you post the whole project (in zip archive not including .aps, .clw, .ncb files nor Debug & Release folders)?
    Victor Nijegorodov

  5. #5
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: Problem with Polygon in Stroustrup's PPP book

    Quote Originally Posted by r.abbasi View Post
    Hi guys,

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

    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 run the code I get 13 errors which must of them are about the Polygon identifier.
    The problem in that code has to do with Polygon poly; and the rest of the using of it. For example first error is: Error C2872: 'Polygon' : ambiguous symbol

    Why my compiler doesn't know that Polygon please?
    Ambiguous symbol implies that you have two different Ploygons defined and the compiler doesn't know which one you mean.

  6. #6
    Join Date
    Jan 2014
    Posts
    7

    Re: Problem with Polygon in Stroustrup's PPP book

    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:raw(void)" (?draw@Window@Graph_lib@@MAEXXZ) C:\Users\CS\documents\visual studio 2012\Projects\Win32Project1\Win32Project1\Win32Project1.obj

  7. #7
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: Problem with Polygon in Stroustrup's PPP book

    Now you have a linker error which means that a library or object module containing Graph_lib::Polygon is absent!
    Victor Nijegorodov

  8. #8
    Join Date
    Jan 2014
    Posts
    7

    Re: Problem with Polygon in Stroustrup's PPP book

    OK I inputted graph_lib.lib before fltk.lib separated by new line in the linker input. Now the list of linker input is like this:
    graph_lib.lib fltkd.lib wsock32.lib comctl32.lib fltkjpegd.lib fltkimagesd.lib

    This time the error is:
    Error 1 error LNK1104: cannot open file 'graph_lib.lib' C:\Users\CS\documents\visual studio 2012\Projects\Win32Project1\Win32Project1\LINK

    You can read the detailed version of this problem here:
    http://stackoverflow.com/questions/2...troustrups-ppp

  9. #9
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: Problem with Polygon in Stroustrup's PPP book

    Quote Originally Posted by r.abbasi View Post
    OK I inputted graph_lib.lib
    This time the error is:
    Error 1 error LNK1104: cannot open file 'graph_lib.lib' C:\Users\CS\documents\visual studio 2012\Projects\Win32Project1\Win32Project1\LINK
    Well this is very clear: "the file 'graph_lib.lib' cannot open" likely means the file is not tn the C:\Users\CS\documents\visual studio 2012\Projects\Win32Project1\Win32Project1\LINK folder.
    Where is this file?
    Try to add its folder to the "additional directories" or put the full path name in the linker input.
    Victor Nijegorodov

  10. #10
    Join Date
    Jan 2014
    Posts
    7

    Re: Problem with Polygon in Stroustrup's PPP book

    First off, there isn't any file named graph_lib.lib in my C:\ drive (where the OS and visual studio are installed on it) and I think this is the reason that compiler con't open it. If so why the code has used that library!!?

    >Try to add its folder. What folder do you mean?

    > put the full path name in the linker input.. Of what? The graph_lib? But I haven't found it yet to add it.

  11. #11
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: Problem with Polygon in Stroustrup's PPP book

    Have you installed the FLTK lib as detailed in Appendix D?
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  12. #12
    Join Date
    Jan 2014
    Posts
    7

    Re: Problem with Polygon in Stroustrup's PPP book

    For knowing how I installed the FLTK, please read that link. I have wrote the instructions step-by-step in detail in that link. (The link written in post #8).

  13. #13
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Problem with Polygon in Stroustrup's PPP book

    Quote Originally Posted by r.abbasi View Post
    > put the full path name in the linker input.. Of what? The graph_lib? But I haven't found it yet to add it.
    Why did you assume that the library name is "graph_lib.lib"? It could be another name, or it may not even be a library, but another set of source modules that need to be compiled. Don't just add library names to the linker settings if that library is non-existent.

    Edit:
    http://www.cplusplus.com/forum/beginner/48650/

    Did you forget to compile GUI.cpp?

    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; January 12th, 2014 at 11:34 AM.

  14. #14
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: Problem with Polygon in Stroustrup's PPP book

    graph_lib is the name of the namespace, not the name of a library.

    You also need to add the .cpp files you downloaded from Stroustrup's web site into your project so they are compiled with it.

    PS I followed your steps using MSVC and got the same issues you have. After adding the Stroustrup's .cpp downloaded files to the project and then building the project I got no errors and running the produced .exe gave the expected graphics window.
    Last edited by 2kaud; January 12th, 2014 at 01:55 PM.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  15. #15
    Join Date
    Jan 2014
    Posts
    7

    Re: Problem with Polygon in Stroustrup's PPP book

    Quote Originally Posted by 2kaud View Post
    graph_lib is the name of the namespace, not the name of a library.

    You also need to add the .cpp files you downloaded from Stroustrup's web site into your project so they are compiled with it.

    PS I followed your steps using MSVC and got the same issues you have. After adding the Stroustrup's .cpp downloaded files to the project and then building the project I got no errors and running the produced .exe gave the expected graphics window.
    THANK YOU VERY VERY MUCH DEAR "@Kaud": HORRRRRAAAAAA.:thumb
    I added all the .cpp files of the GUI folder that I have downloaded them to the project (they are Graph.cpp, GUI.cpp, Simple_window.cpp and Window.cpp) and ran the project. It successfully showed the result. Thank you all.
    Last edited by r.abbasi; January 13th, 2014 at 05:04 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured