"Programming Principles and Practices" errors

I have a pretty big problem with Chapter 12 of "Programming Principles and Practices". Well, in that chapter he introduces graphics. He uses FLTK, which i have downloaded and installed successfully. However, I have a problem with HIS headers. If you don't remember them :
http://www.stroustrup.com/Programming/Graphics/ - from his site.
It's about "Graph.h".
I cannot compile because I get error :
"error C2955: 'Vector' : use of class template requires template argument list".
I have found some "related" problems on : http://cboard.cprogramming.com/cplusplus-programming/139936-libraries-bjarne-stroustrups-book-wont-work.html , but it doesn't solve my problem.

If you are familiar with that error or have some info connected to it, please let me know.

Vector should be Vector_ref And there's a vector just below (?) which should also be a Vector_ref

(this looks like someone meant to add the standard no-copy incantation and got it wrong!)

Andy

P.S. I've got it to compile... But there are a number of errors in the code which are a bit disappointing to find in a sample. It looks like someone refactored the source to tidy it up, before making it public, but did not check that it still compiled.

Errors:
- 2 methods which were defined inline in the class and again in the cpp file: Circle's constructior and center() method, both defined in Graph.h/.cpp
- Simple_window base class Window being an ambiguous symbol. (Graph_lib::Window vs Window in FLTK, a typedef of HWND) : I fixed this by (a) commenting out the using namespace in Simple_Windows.h and namespace qualifying all the types (Graph_lib::Window, Graph_lib::Button, Graph_lib::Address) and then (b) adding using namespace Graph_lib; just below the #include's in Simple_window.cpp

(this latter prob. might not happen with FLTK 1.1, but FLTK 1.3 is the current stable release.)

Warnings:
There were a number of small defects which shouldn't have been there. Generally
- order of class member variable declaration cf. their order in the initializer list)
- use of int as the loop variable when testing against vector::size() -- when they should be size_t (or maybe a re-typedef of std::vector::size_type).

There was also a warning from gcc about hash_map begin deprecated. When I tried to switch to unordered_map it warned me that was still a prototype. So I went bask to hash+_map and added -Wno-deprecated to the compiler options.
Last edited on
Note I did not add <string.h> as grumpy suggested in the cboard thread mentioned by bl4cbb3rry. But that's because I had FLTK 1.3 on my path already. grep-ing the code I can see Graph.h/.cpp are using FLTK (e.g. FL_RED, FL_GREEN, FL_BLUE, ... which are FLTK's color consts)

But it's benign - and that bit clearer - to add it, should you feel the need.
Last edited on
To fix the errors and warnings, I did the following:

Graph.h

#1 warning: swapped the order of variable declaration in struct Color to

1
2
    Fl_Color c;
    char v;    // invisible and visible for now 


#2 warning: altered index type for the loop in ~Vector_ref() to size_t

#3 warning: altered size() to return a size_t

#4 error: fixed the no-copy code to

1
2
3
private:	// prevent copying
	Vector_ref(const Vector_ref&);
	Vector_ref& operator=(const Vector_ref&);


#5 warning: swapped the order of variable declaration in struct Rectangle to

1
2
    int w;    // width
    int h;    // height 


#6 error: removed the body of struct Circle, constructor, leaving definition in cpp file

#7 error: ditto for Circle's center() method

Graph.cpp

#1 warning: changed type of index variable for loop in Shape::draw_lines() to size_t.

#2 warning: ditto for Shape::move()

#3 warning: ditto for get_encoding()

Simple_Window.h

#1 error: commenting out the using namespace Graph_lib;

#2 error: namespace qualified all the types variables with Graph_lib types : Graph_lib::Window, Graph_lib::Button, Graph_lib::Address)

Simple_Window.cpp

#1 error: added using namespace Graph_lib; just below the #include's

Window.cpp

#1 warning: changed type of index variable for loop in Window::draw() to size_t.

#2 warning: changed type of index variable for loop in Window::put_on_top() to size_t. )

Makefile.txt

#1 misc: renamed it to just "makefile"

#2 warning: added -Wno-deprecated to the compiler command line

I also added FLTK=/local/include to the top of the makefile, so I did not have to modify my existing build environment, which in MinGW in this case (with FLTK installed in the default location using "make install")
Last edited on
Well, thank you Andy, I removed most of the errors.... But after I've done all that, I started getting this error:

Simple_window.obj : error LNK2001: unresolved external symbol "public: virtual void __thiscall
Graph_lib::Button::attach(class Graph_lib::Window &)" (?attach@Button@Graph_lib@@UAEXAAVWindow@2@@Z)
This is when building libbookgui.a?

I don't see this error, and I can see Button::attach is defined correctly.

Are you using gcc? Or vc? Or?

EDIT: going by the linker error, it's vc!

Andy

GUI.h

1
2
3
4
5
6
7
    struct Button : Widget {
        Button(Point xy, int w, int h, const string& label, Callback cb)
            : Widget(xy,w,h,label,cb)
        {}

        void attach(Window&);
    };


GUI.cpp

1
2
3
4
5
6
void Button::attach(Window& win)
{
    pw = new Fl_Button(loc.x, loc.y, width, height, label.c_str());
    pw->callback(reinterpret_cast<Fl_Callback*>(do_it), &win); // pass the window
    own = &win;
}

Last edited on
*while looking at his feet he stutters:*
I haven't put GUI.cpp into my project, only GUI.h, so that's why I got linker error.

After that I compiled it successfully. Thank you so much!
Have you downloaded the zip file with all the source for the chapters and FLTK, etc?

(I mean the "Complete collection of code fragments (revised)" link)

I can see the source for the GUI files, which we're been talking about, are a little bit different.
Last edited on
Topic archived. No new replies allowed.