Section13.7 getting error when using Open_polyline

76 views
Skip to first unread message

cortical_iv

unread,
Jun 17, 2018, 11:52:09 PM6/17/18
to PPP-public
I initially was getting an error because I was inheriting the constructors from Shape:
    using Shape::Shape;
Based on lots of discussion here at this forum from Christano and his code, I replaced that by just declaring constructors:

    Open_polyline() {}
    Open_polyline(initializer_list<Point> lst): Shape{lst} {}

Unfortunately, now when I try to compile/run the code in Section 13.7, I am getting the following error:


main.cpp:: undefined reference to `Graph_lib::Shape::Shape(std::initializer_list<Graph_lib::Point>)'


This same error happens with various versions of the same basic main.cpp lines:


Open_polyline open_poly1 = { {10,20}, { 40, 333} };
win.attach(open_poly1);


And also:

vector<Point> my_points {Point{100, 100}, Point{150, 200}};
Open_polyline open_poly1 = {my_points[0], my_points[1]};


Interestingly, the following works fine:


 vector<Point> my_points {Point{100, 100}, Point{150, 200}};
Open_polyline open_poly1;
open_poly1.add(my_points[0]);
open_poly1.add(my_points[1]);



cortical_iv

unread,
Jun 18, 2018, 10:07:22 AM6/18/18
to PPP-public
Just to clarify, I am compiling/running in Qt Creator. So far everything has worked fine for other projects in Chapters 12/13. When I start the build process for this project (named 'polys') here is the full message:

9:49:16: Running steps for project polys...
09:49:16: Starting: "/usr/bin/cmake" --build . --target all
Scanning dependencies of target polys
[ 16%] Building CXX object CMakeFiles/polys.dir/main.cpp.o
[ 33%] Linking CXX executable polys
CMakeFiles/polys.dir/main.cpp.o: In function `Graph_lib::Open_polyline::Open_polyline(std::initializer_list<Graph_lib::Point>)':
main.cpp:(.text._ZN9Graph_lib13Open_polylineC2ESt16initializer_listINS_5PointEE[_ZN9Graph_lib13Open_polylineC5ESt16initializer_listINS_5PointEE]+0x30): undefined reference to `Graph_lib::Shape::Shape(std::initializer_list<Graph_lib::Point>)'
CMakeFiles/polys.dir/build.make:214: recipe for target 'polys' failed
CMakeFiles/Makefile2:67: recipe for target 'CMakeFiles/polys.dir/all' failed
Makefile:83: recipe for target 'all' failed
collect2: error: ld returned 1 exit status
make[2]: *** [polys] Error 1
make[1]: *** [CMakeFiles/polys.dir/all] Error 2
make: *** [all] Error 2
09:49:17: The process "/usr/bin/cmake" exited with code 2.
Error while building/deploying project polys (kit: Desktop)
When executing step "CMake Build"

It's only a problem when I try to give it initializer lists. It works fine when I use the 'add' method to add points to the polyline.

Christiano SA

unread,
Jun 18, 2018, 12:06:55 PM6/18/18
to ppp-p...@googlegroups.com
It is working here.

12.cpp:

#include "Simple_window.h"
#include "Graph.h"

int main()
{
using Graph_lib::Open_polyline;

Point tl{ 100,100 };
Simple_window win{ tl,600,400,"Test" };
Open_polyline open_poly1 = { {10,20}, { 40, 333} };
win.attach(open_poly1);
win.wait_for_button();

return 0;
}

library:

https://groups.google.com/forum/#!topic/ppp-public/Url2jZtSrVQ

Makefile:

CC=g++

all: 12
@echo "Done."


12: Graph.o Window.o GUI.o Simple_window.o 12.o
$(CC) -std=c++14 -L/usr/local/lib Graph.o Window.o GUI.o Simple_window.o 12.o -o 12 -lfltk -lfltk_images

12.o: 12.cpp Simple_window.h GUI.h Window.h fltk.h std_lib_facilities.h Point.h Graph.h
$(CC) -std=c++14 -I/usr/local/include -c 12.cpp

Graph.o: Graph.cpp Graph.h Point.h fltk.h std_lib_facilities.h
$(CC) -std=c++14 -I/usr/local/include -c Graph.cpp

Window.o: Window.cpp Window.h fltk.h std_lib_facilities.h Point.h Graph.h GUI.h
$(CC) -std=c++14 -I/usr/local/include -c Window.cpp

GUI.o: GUI.cpp GUI.h Window.h fltk.h std_lib_facilities.h Point.h Graph.h
$(CC) -std=c++14 -I/usr/local/include -c GUI.cpp

Simple_window.o: Simple_window.cpp Simple_window.h GUI.h Window.h fltk.h std_lib_facilities.h Point.h Graph.h
$(CC) -std=c++14 -I/usr/local/include -c Simple_window.cpp

clean:
rm 12 *.o *.core


> Sent: Monday, June 18, 2018 at 12:52 AM
> From: cortical_iv <thomso...@gmail.com>
> To: PPP-public <ppp-p...@googlegroups.com>
> Subject: Section13.7 getting error when using Open_polyline
> --
> You received this message because you are subscribed to the Google Groups "PPP-public" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to ppp-public+...@googlegroups.com.
> To post to this group, send email to ppp-p...@googlegroups.com.
> Visit this group at https://groups.google.com/group/ppp-public.
> For more options, visit https://groups.google.com/d/optout.
>
Screenshot from 2018-06-18 12-59-00.png

cortical_iv

unread,
Jun 18, 2018, 3:36:58 PM6/18/18
to PPP-public
Crazy when I use it with the build I made using all the random stuff I found on the interwebs, it doesn't work. But when I do it using your build (12new), it works! Ugh I tried to follow this to make my build:


But I think yours is more stable. Does yours work all the way through Chapter 16? I am obviously just in Ch 13...Thanks so much.

Christiano SA

unread,
Jun 18, 2018, 4:58:21 PM6/18/18
to ppp-p...@googlegroups.com
Yes. It works with chapter 12 to 16. If you find a bug, please let me know.
I tried to keep the library as close to the 2nd edition book as possible, and the possible bugs in the book I put in the bugs.html.
 
Sent: Monday, June 18, 2018 at 4:36 PM

From: cortical_iv <thomso...@gmail.com>
To: PPP-public <ppp-p...@googlegroups.com>
Subject: Re: Section13.7 getting error when using Open_polyline
--

cortical_iv

unread,
Jun 18, 2018, 6:42:23 PM6/18/18
to PPP-public


On Monday, June 18, 2018 at 4:58:21 PM UTC-4, Christiano wrote:
Yes. It works with chapter 12 to 16. If you find a bug, please let me know.
I tried to keep the library as close to the 2nd edition book as possible, and the possible bugs in the book I put in the bugs.html.
 

You, sir, are a hero!

Do you have this in a repository at github? And do you mind if I convert my version over to yours (with credit, of course)?  Right now this is what I'm using:


But yours is better.

Christiano SA

unread,
Jun 18, 2018, 7:31:31 PM6/18/18
to ppp-p...@googlegroups.com
I was at beginning working with this version:
https://groups.google.com/d/msg/ppp-public/BtlzdWGuQpQ/KuDN4u-SPKgJ
But I had problems at some time.
So, I decided to modify the PPP1 library so that it could be close to 2nd Edition book using the book itself and parts of the Problematic prior library. This is how the ch12new was born.

I don't mind if you modify it, but the users might like to know the modifications. You might write a section like "Difference between it and the ch12new" in your repository.

> Sent: Monday, June 18, 2018 at 7:42 PM
> From: cortical_iv <thomso...@gmail.com>
> To: PPP-public <ppp-p...@googlegroups.com>
> Subject: Re: Section13.7 getting error when using Open_polyline
>
>
>

cortical_iv

unread,
Jun 18, 2018, 8:37:31 PM6/18/18
to PPP-public


On Monday, June 18, 2018 at 7:31:31 PM UTC-4, Christiano wrote:
I was at beginning working with this version:
https://groups.google.com/d/msg/ppp-public/BtlzdWGuQpQ/KuDN4u-SPKgJ
But I had problems at some time.
So, I decided to modify the PPP1 library so that it could be close to 2nd Edition book using the book itself and parts of the Problematic prior library. This is how the ch12new was born.

I don't mind if you modify it, but the users might like to know the modifications. You might write a section like "Difference between it and the ch12new" in your repository.

 
I was thinking more I would just put your version on github without modification (other than adding a CMakeLists.txt file for people -- like me -- that prefer to build using cmake).  I will link to your page here where you gave all the attachments to give proper attribution. I have to go to store now, but will put up link when I finish this: clearly you have created the best version of this code to use!

cortical_iv

unread,
Jun 18, 2018, 10:46:52 PM6/18/18
to PPP-public
On Monday, June 18, 2018 at 7:31:31 PM UTC-4, Christiano wrote:
I was at beginning working with this version:
https://groups.google.com/d/msg/ppp-public/BtlzdWGuQpQ/KuDN4u-SPKgJ
But I had problems at some time.
So, I decided to modify the PPP1 library so that it could be close to 2nd Edition book using the book itself and parts of the Problematic prior library. This is how the ch12new was born.

I don't mind if you modify it, but the users might like to know the modifications. You might write a section like "Difference between it and the ch12new" in your repository.


Now I understand because in that thread you had can_open return:

return static_cast<bool>(ff);

But in this version you have:

    ifstream ff(s);
    return ff.is_open();

Now I'm seeing some of these differences in the merges. It seems you put a ton of work in and really have made a beautiful code base!

cortical_iv

unread,
Jun 19, 2018, 10:22:24 AM6/19/18
to PPP-public
On Monday, June 18, 2018 at 7:31:31 PM UTC-4, Christiano wrote:
I don't mind if you modify it, but the users might like to know the modifications. You might write a section like "Difference between it and the ch12new" in your repository.

I put up your code at github:

Note because it is github, people will easily be able to see modifications in the version history there is no need for separate documentation of things like that. That's the beauty of VCS! But as it currently stands, it is your code without modification except I've added the ability to build using cmake (the folks at fltk helped me a lot getting that working and now that I have understand it, I really like using cmake).

Art Werschulz

unread,
Jun 20, 2018, 8:43:14 AM6/20/18
to ppp-p...@googlegroups.com
Hi.

I'm fiddling around with this a bit.  In particular, I want to separate the building and installation of the library from its use in client code (much as Bjarne does in his version).  I'm also trying to create Makefiles that work on Linux and on Mac OS X.

As an additional bit of paranoia, I'm adding the -Wall flag to the C++ command line options. When building the library, I ran into two kinds of warnings:

agw@home:hello_fltk-master$ make main.o
g++   -std=c++14 -Wall -I/usr/local/include -c main.cpp
In file included from main.cpp:1:
In file included from ./Simple_window.h:10:
In file included from ./GUI.h:10:
./Window.h:38:14: warning: 'Graph_lib::Window::resize' hides overloaded virtual
      function [-Woverloaded-virtual]
        void resize(int ww, int hh) { w=ww, h=hh; size(ww,hh); }
             ^
/usr/local/include/FL/Fl_Window.H:261:16: note: hidden overloaded virtual
      function 'Fl_Window::resize' declared here: different number of parameters
      (4 vs 2)
  virtual void resize(int X,int Y,int W,int H);
               ^
In file included from main.cpp:1:
In file included from ./Simple_window.h:10:
In file included from ./GUI.h:11:
./Graph.h:45:27: warning: field 'c' will be initialized after field 'v'
      [-Wreorder]
    Color(Color_type cc) :c(Fl_Color(cc)), v(visible) { }
                          ^
./Graph.h:46:44: warning: field 'c' will be initialized after field 'v'
      [-Wreorder]
    Color(Color_type cc, Transparency vv) :c(Fl_Color(cc)), v(vv) { }
                                           ^
./Graph.h:47:20: warning: field 'c' will be initialized after field 'v'
      [-Wreorder]
    Color(int cc) :c(Fl_Color(cc)), v(visible) { }
                   ^
./Graph.h:48:29: warning: field 'c' will be initialized after field 'v'
      [-Wreorder]
    Color(Transparency vv) :c(Fl_Color{}), v(vv) { }    // default color
                            ^
5 warnings generated.

The initialization ordering issue is not merely stylistic, see the last comment in https://stackoverflow.com/questions/30364585/will-be-initialized-after-wreorder.  OTOH, I don't see that this will cause a problem in normal use.  There are two ways to remove the warning msg:

(1) The right thing to do is to swap lines 55 and 56 in Graph.h.
(2) Alternatively, leave Graph.h alone and to add -Wno-reorder to the C++ command line options.

I'm not going to try fixing the hidden virtual function problem.  (In my Makefile, I've added -Wno-overloaded-virtual to my C++ command line options, which surpresses this warning msg.)  

So I'm asking the maintainer (Eric?) to make these changes (i.e., add -Wall and -Wno-overloaded-virtual to the C++ command line options, and either fix Graph.h or add -Wno-reorder to the C++ command line options).

Thanks.

Art Werschulz

cortical_iv

unread,
Jun 20, 2018, 9:37:04 AM6/20/18
to PPP-public
Thanks for this input. I will mention the -Wall option, though frankly I avoid it with the code in this book because I got sick of the compiler yelling at me. :)
I will swap c and v declarations (lines 55 and 56).

The virtual function problem I don't really understand enough to have an opinion about the best thing to do. We should probably think about it more. E.g, consider https://stackoverflow.com/questions/18515183/c-overloaded-virtual-function-warning-by-clang

At work now so will do this when done with work.



Note while pointing things out here is fine, it would also be helpful to create issues at the respository so others can see what has been done: https://github.com/cortical-iv/hello_fltk/issues




Christiano SA

unread,
Jun 20, 2018, 11:05:22 AM6/20/18
to ppp-p...@googlegroups.com
I've attached a new version without warnings and a screenshot with final chapter 12 example. It was tested with Clang++ and G++.
 
 
Sent: Wednesday, June 20, 2018 at 9:43 AM
From: "Art Werschulz" <a...@comcast.net>
To: ppp-p...@googlegroups.com
Subject: christiano's revision of the PPP GUI
--
ch12new20180620.tar.gz
Screenshot from 2018-06-20 11-53-45.png

Art Werschulz

unread,
Jun 20, 2018, 11:43:05 AM6/20/18
to PPP-public
Hi.

On Jun 20, 2018, at 11:05 AM, Christiano SA <chris...@engineer.com> wrote:

I've attached a new version without warnings and a screenshot with final chapter 12 example. It was tested with Clang++ and G++.

When I tried this on my Mac (clang++), I got a passel of load errors .  The fix was to replace the line

$(CC) -std=c++14 -L/usr/local/lib Graph.o Window.o GUI.o Simple_window.o 12.o -o 12 -lfltk -lfltk_images 

with

$(CC) -std=c++14 -L/usr/local/lib Graph.o Window.o GUI.o Simple_window.o 12.o -o 12 `fltk-config --ldflags --use-images`

in the Makefile.


Art Werschulz



cortical_iv

unread,
Jun 21, 2018, 1:09:27 AM6/21/18
to PPP-public


On Wednesday, June 20, 2018 at 11:05:22 AM UTC-4, Christiano wrote:
I've attached a new version without warnings and a screenshot with final chapter 12 example. It was tested with Clang++ and G++.
 
 
HOw did you fix these warnings?
In file included from ./GUI.h:10:
./Window.h:38:14: warning: 'Graph_lib::Window::resize' hides overloaded virtual
      function [-Woverloaded-virtual]

Also when I turned on -Wall I got a bunch of errors of the form:
Graph.h:121:36: warning: comparison of integer expressions of different signedness: ‘int’ and ‘std::vector<Graph_lib::Button*, std::allocator<Graph_lib::Button*> >::size_type’ {aka ‘long unsigned int’} [-Wsign-compare

Christiano SA

unread,
Jun 21, 2018, 7:23:11 AM6/21/18
to ppp-p...@googlegroups.com
When you have two files and you want to know the difference between them, you can use the command diff:
 
$ diff ch12new/Window.h ch12new_old/ch12new/Window.h
38d37
<     using Fl_Window::resize;   // To avoid warnings (clang)
 
The source of this line is:
 
Sent: Thursday, June 21, 2018 at 2:09 AM

From: cortical_iv <thomso...@gmail.com>
To: PPP-public <ppp-p...@googlegroups.com>
Subject: Re: christiano's revision of the PPP GUI
--

Art Werschulz

unread,
Jun 21, 2018, 10:19:18 AM6/21/18
to ppp-p...@googlegroups.com
Hi.

The working program 12.cpp demonstrates a Function constructor using a named function as its first parameter.

Has anybody gotten the Function constructor to work with lambdas?  I pasted the following code (based on Bjarne's chapter15.3.3.cpp) into 12.cpp:

    const Point orig(300,400);
    Function s1{[](double x) {return 1.0;},-10,11,orig,400,30,30};
    win.attach(s1);

<aside>
I needed to use Bjarne's Point.h here, since I needed a two-parameter constructor for Point.
</aside>

I then found the following:

agw@home:ch12new$ make
g++ -Wall -std=c++14 -I/usr/local/include -c 12.cpp
12.cpp:31:14: error: call to constructor of 'Graph_lib::Function' is ambiguous
    Function s1{[](double x) {return 1.0;},
             ^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
./Graph.h:174:5: note: candidate constructor
    Function(Fct f, double r1, double r2, Point orig,
    ^
./Graph.h:176:5: note: candidate constructor
    Function(double (*f)(double), double r1, double r2, Point orig,
    ^
1 error generated.
make: *** [12.o] Error 1

Has anybody made any progress on this?

Thanks.

Art Werschulz



Christiano SA

unread,
Jun 21, 2018, 1:32:55 PM6/21/18
to ppp-p...@googlegroups.com
Art, do you remember that thread?:
 
There are three manners to use Function, for example:
    int n = 10;
    Function f1( sin, 0, 100, Point{ 20,150 }, 1000, 50, 50 );
    Function f2( [](double x) {return x;}, 0, 100, Point{ 20,150 }, 1000, 50, 50 );
    Function f3( [n](double x) {return n*x; }, 0, 100, Point{ 20,150 }, 1000, 50, 50 );
 
The first is using pointer to function, sin for example.
The second is using lambda without capture variables.
The third is using lambda with capture variables.
 
My message of day 8/27/17 tried to solve everything without the necessity of write static_cast<double (*)(double)>.
The problem is: I hadn't noticed that the problem persists when lambda doesn't have capture variables, as your example.
 
The problem is;
The closure type for a lambda-expression with no lambda-capture has a public non-virtual non-explicit const conversion function to pointer to function having the same parameter and return types as the closure type’s function call operator.
 
So, my solution now is:
 
Fct is defined as:
typedef std::function<double (double)> Fct;
 
(as I had said in message 6/22/17)
 
But I have removed the line
using namespace std;
from std_lib_facilities.h
and added this:
using std::string;
using std::runtime_error;
using std::vector;
using std::initializer_list;
using std::pair;
using std::ifstream;
 
As you can see, now the three ways are working:
 
Program:
 
#include "Simple_window.h"
#include "Graph.h"
int main()
{
    using Graph_lib::Function;
    Point tl{ 100,100 };
    Simple_window win{ tl,600,400,"Canvas" };
        
    int n = 10;
    Function f1( sin, 0, 100, Point{ 20,150 }, 1000, 50, 50 );
    Function f2( [](double x) {return x;}, 0, 100, Point{ 20,150 }, 1000, 50, 50 );
    Function f3( [n](double x) {return n*x; }, 0, 100, Point{ 20,150 }, 1000, 50, 50 );
    win.attach(f1);
    win.attach(f2);
    win.attach(f3);
    win.set_label("Test");
    win.wait_for_button();
    return 0;
}
 
 
I have tested with linux, clang++ and g++. Can someone test it on Windows?
 
WARNINGs:
You should not add "using namespace std;" because it forces the sin function to have overloaded versions (std::sin) instead only double (double) from global space (you would be forced to write static_cast<double (*)(double)> as previously stated).
 
The new version is attached.
 
Sent: Thursday, June 21, 2018 at 11:19 AM
From: "Art Werschulz" <a...@comcast.net>
To: ppp-p...@googlegroups.com

Subject: Re: christiano's revision of the PPP GUI
--
ch12new20180621.tar.gz
Reply all
Reply to author
Forward
0 new messages