-
January 8th, 2014, 04:06 AM
#1
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?
-
January 8th, 2014, 04:55 AM
#2
Re: Problem with Polygon in Stroustrup's PPP book
And where (in what headers) is Polygon defined? And how?
Victor Nijegorodov
-
January 8th, 2014, 07:48 AM
#3
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.
-
January 8th, 2014, 08:04 AM
#4
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
-
January 8th, 2014, 08:25 AM
#5
Re: Problem with Polygon in Stroustrup's PPP book
Originally Posted by r.abbasi
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.
-
January 11th, 2014, 04:46 AM
#6
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
-
January 12th, 2014, 05:50 AM
#7
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
-
January 12th, 2014, 07:31 AM
#8
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
-
January 12th, 2014, 07:42 AM
#9
Re: Problem with Polygon in Stroustrup's PPP book
Originally Posted by r.abbasi
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
-
January 12th, 2014, 08:37 AM
#10
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.
-
January 12th, 2014, 11:26 AM
#11
Re: Problem with Polygon in Stroustrup's PPP book
Originally Posted by r.abbasi
> 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.
-
January 12th, 2014, 08:45 AM
#12
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)
-
January 12th, 2014, 09:25 AM
#13
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).
-
January 12th, 2014, 01:12 PM
#14
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)
-
January 13th, 2014, 04:56 AM
#15
Re: Problem with Polygon in Stroustrup's PPP book
Originally Posted by 2kaud
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|