1

Hello members of stack overflow! I am hoping someone who has been where I am can point me in the right direction.

I am currently learning C++ and working on the graphics exercises found in chapter 12 but I cannot seem to get code that should work to work. I have been attempting for days to get any code to work and have overcome some big problems already but since I am only learning C++, I am at this point at the limit of my expertise and have exhausted all previously asked questions on various forums which actually solved a lot of other problems I was having.

Here is the simple graphics code and below that the large amount of error messages I receive:

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

#include <PPP2HEADERS/Simple_window.h>// get access to our window library
#include <PPP2HEADERS/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

    Graph_lib::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
}

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

error messages received when attempting to compile:

1>------ Build started: Project: test2, Configuration: Debug Win32 ------
1>test4.cpp
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Auxiliary\VS\include\PPP2HEADERS\Graph.h(45,10): warning C4305: 'initializing': truncation from 'Graph_lib::Color::Transparency' to 'char'
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Auxiliary\VS\include\PPP2HEADERS\Graph.h(45,46): warning C4309: 'initializing': truncation of constant value
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Auxiliary\VS\include\PPP2HEADERS\Graph.h(47,10): warning C4305: 'initializing': truncation from 'Graph_lib::Color::Transparency' to 'char'
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Auxiliary\VS\include\PPP2HEADERS\Graph.h(47,39): warning C4309: 'initializing': truncation of constant value
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Auxiliary\VS\include\PPP2HEADERS\GUI.h(107,39): warning C4018: '<': signed/unsigned mismatch
1>C:\Users\darai\Documents\PPP C++ CODE\chapter12_graphics_example\test2\test4.cpp(16,23): error C2440: 'initializing': cannot convert from 'initializer list' to 'Graph_lib::Point'
1>C:\Users\darai\Documents\PPP C++ CODE\chapter12_graphics_example\test2\test4.cpp(16,23): message : No constructor could take the source type, or constructor overload resolution was ambiguous
1>C:\Users\darai\Documents\PPP C++ CODE\chapter12_graphics_example\test2\test4.cpp(22,28): error C2440: '<function-style-cast>': cannot convert from 'initializer list' to 'Graph_lib::Point'
1>C:\Users\darai\Documents\PPP C++ CODE\chapter12_graphics_example\test2\test4.cpp(22,28): message : No constructor could take the source type, or constructor overload resolution was ambiguous
1>C:\Users\darai\Documents\PPP C++ CODE\chapter12_graphics_example\test2\test4.cpp(23,28): error C2440: '<function-style-cast>': cannot convert from 'initializer list' to 'Graph_lib::Point'
1>C:\Users\darai\Documents\PPP C++ CODE\chapter12_graphics_example\test2\test4.cpp(23,28): message : No constructor could take the source type, or constructor overload resolution was ambiguous
1>C:\Users\darai\Documents\PPP C++ CODE\chapter12_graphics_example\test2\test4.cpp(24,28): error C2440: '<function-style-cast>': cannot convert from 'initializer list' to 'Graph_lib::Point'
1>C:\Users\darai\Documents\PPP C++ CODE\chapter12_graphics_example\test2\test4.cpp(24,28): message : No constructor could take the source type, or constructor overload resolution was ambiguous
1>Done building project "test2.vcxproj" -- FAILED.
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

I am pretty sure FLTK is installed correctly as there is an example code found in appendix D of the book which does compile and I am also using the updated headers as mentioned here.

At this point i have been attempting to get graphics code to work for days and I am wondering if learning the graphics portion of the book is absolutely necessary.

Any help anyone can provide would be hugely appreciated and apologies if I have broken any rules, this is my first time posting to stack overflow.

Thank you.

Point.H as requested:

//
// This is a GUI support code to the chapters 12-16 of the book
// "Programming -- Principles and Practice Using C++" by Bjarne Stroustrup
//

#ifndef POINT_GUARD
#define POINT_GUARD

namespace Graph_lib {
//------------------------------------------------------------------------------

struct Point {
    int x, y;
};

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

inline bool operator==(Point a, Point b) { return a.x==b.x && a.y==b.y; } 

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

inline bool operator!=(Point a, Point b) { return !(a==b); }

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

}
#endif // POINT_GUARD
Dara
  • 11
  • 2
  • Please edit your post to show the exact, full contents of `chapter12_graphics_example\test2\test4.cpp`. The code you have posted does not match the error messages you have posted. Also please post the class declaration for `Point` in one of your header files so we can see what constructors it has. Note some of the "warnings" you get are probably because the code was written a long time ago, but the "errors" need to be fixed to build this program. – John Zwinck Oct 24 '20 at 00:35
  • @JohnZwinck My full code is shown, the only changes include additional comments at the top. Point.H is also posted. thank you for your help. – Dara Oct 29 '20 at 23:24

1 Answers1

0

Assuming the following directory layout, adjust as necessary. Also assuming that the FLTK libraries in fltk\lib have been successfully built, following e.g. Install-FLTK-for-use-with-Visual-C.

X:\ppp2code
├─fltk     <--  FLTK root directory, based on fltk-1.3.5-source
│ ├─FL     <--       include directory
│ └─lib    <--       lib directory, with the prebuilt libraries
├─gui      <--  PPP2 GUI (simple_window etc), based on stroustrup.com/programming_support.html 
└─test4    <--  OP's test code
  • Replace #include <PPP2HEADERS/Simple_window.h> with #include <Simple_window.h> in test4.cpp, and similarly #include <PPP2HEADERS/Graph.h> with #include <Graph.h>.

  • Open the x86 Native Tools Command Prompt for VS 2019 (cmd /k vcvars32.bat) with the current directory set to X:\ppp2code.

  • Compile with /W3 to avoid the (mostly innocuous) warnings triggered by /W4.

    X:\ppp2code\test4>cl.exe /c /nologo /W3 /D "WIN32" /MD /EHsc /O2 /I "..\gui" /I "..\fltk" test4.cpp ..\gui\*.cpp
    
  • Link with the supporting PPP2 objects and the prebuilt FLTK libraries.

    X:\ppp2code\test4>link /nologo /LIBPATH:"..\fltk\lib" /SUBSYSTEM:WINDOWS /OUT:"test4.exe" test4.obj graph.obj gui.obj simple_window.obj window.obj fltk.lib fltkimages.lib fltkjpeg.lib user32.lib gdi32.lib shell32.lib advapi32.lib ole32.lib
    
  • Run the resulting test4.exe.

dxiv
  • 16,984
  • 2
  • 27
  • 49
  • Hello dxiv, thank you for your help and apologies for the slow reply. Unfortunately i am not entirely sure what you want me to do here. Do you mean for me to copy the fltk root directory files and the GUI files into the project folder, in this case test2? thank you again for your help – Dara Oct 29 '20 at 23:16
  • @Dara "*adjust as necessary*" means change the `..\fltk` and `..\gui` references in the two command lines to the paths that you use, instead. You shouldn't need to change or copy anything else. – dxiv Oct 30 '20 at 02:47