Problem with Polygon in Stroustrup's PPP book

224 views
Skip to first unread message

ariao...@gmail.com

unread,
Jan 8, 2014, 2:52:28 AM1/8/14
to std-dis...@isocpp.org
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:

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
26
27
28
29
30
31
32
//
// 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?

corn...@google.com

unread,
Jan 8, 2014, 6:06:12 AM1/8/14
to std-dis...@isocpp.org, ariao...@gmail.com
On Wednesday, January 8, 2014 8:52:28 AM UTC+1, ariao...@gmail.com wrote:
Hi guys,


Why my compiler doesn't know that Polygon please?

Hi,

This forum is for discussing the C++ standard. Help with programming is better suited for http://stackoverflow.com and issues with books should be directed to the book's publisher or author.

That said, your compiler does know Polygon. In fact, it knows more than one; that's what "ambiguous" means. It just doesn't know which you mean. You'll have to find out where the other one comes from. (The compiler should be telling you, actually. See if there are any notes after that error.)

Sebastian

ariao...@gmail.com

unread,
Jan 8, 2014, 7:35:45 AM1/8/14
to std-dis...@isocpp.org, ariao...@gmail.com, corn...@google.com

I stated this problem in stackoverflow.com, but no one could tell me a solution so I decided to state it here. All the code is that I've mentioned in my first post. I just run it and it gives me 13 errors. the first is:
Error 8 error C2872: 'Polygon' : ambiguous symbol c:\users\cs\documents\visual studio 2012\projects\testv\testv\testv.cpp 19
and for example next error is this: Error 9 error C2146: syntax error : missing ';' before identifier 'poly' c:\users\cs\documents\visual studio 2012\projects\testv\testv\testv.cpp 19
As you see almost all the errors are about that Polygon. If it help I write all the errors. I don't know where there is another one of that Polygon with the same name and I love to know to get rid of this problem.
And about the Author, what can I do if he doesn't reply?

corn...@google.com

unread,
Jan 8, 2014, 8:20:39 AM1/8/14
to std-dis...@isocpp.org, ariao...@gmail.com, corn...@google.com


On Wednesday, January 8, 2014 1:35:45 PM UTC+1, ariao...@gmail.com wrote:

I stated this problem in stackoverflow.com, but no one could tell me a solution so I decided to state it here.

It's still an inappropriate forum. Having tried an appropriate one first doesn't make it any more appropriate.

If the compiler doesn't tell you, generate the preprocessed code (there's always a switch for that) and do a text search for Polygon.

Vlad from Moscow

unread,
Jan 8, 2014, 9:00:04 AM1/8/14
to std-dis...@isocpp.org, ariao...@gmail.com
If the compiler reports that there is an ambiguous symbol then it also points out where this symbol id declared. So it would be more useful if you would show the complete error message.
 
Also take into account that header "Simple_window.h" already include header "Graph.h". So there is no any need in directive #include "Graph.h".
 
All headers used in this chapter can be downloaded from here
 
 
You can check whether your headers are not corrupted.
 
 

среда, 8 января 2014 г., 11:52:28 UTC+4 пользователь ariao...@gmail.com написал:

ariao...@gmail.com

unread,
Jan 8, 2014, 9:12:50 AM1/8/14
to std-dis...@isocpp.org, ariao...@gmail.com, corn...@google.com
How to generate preprocessed code?

Vlad from Moscow

unread,
Jan 8, 2014, 9:22:21 AM1/8/14
to std-dis...@isocpp.org, ariao...@gmail.com
Also you can try to specify qualified name. For example
 
Graph_lib ::Polygon poly;

среда, 8 января 2014 г., 18:00:04 UTC+4 пользователь Vlad from Moscow написал:

ariao...@gmail.com

unread,
Jan 8, 2014, 9:31:30 AM1/8/14
to std-dis...@isocpp.org, ariao...@gmail.com
Everything is mixed now. I'm going to remove the compiler and the reinstall it again and do works from beginning. 

Vlad from Moscow

unread,
Jan 8, 2014, 9:40:14 AM1/8/14
to std-dis...@isocpp.org, ariao...@gmail.com
Before doing so try to do as I suggested that is try to use the qualified name.

среда, 8 января 2014 г., 18:31:30 UTC+4 пользователь ariao...@gmail.com написал:

ariao...@gmail.com

unread,
Jan 11, 2014, 4:19:51 AM1/11/14
to std-dis...@isocpp.org, ariao...@gmail.com

I used this snip code Graph_lib::Polygon poly; instead of this Polygon poly; and ran 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

ariao...@gmail.com

unread,
Jan 12, 2014, 7:45:35 AM1/12/14
to std-dis...@isocpp.org, ariao...@gmail.com
Any more opinion? 
Reply all
Reply to author
Forward
0 new messages