Stroupstrup Graphics Library Errors

2019-05-22 22:32发布

问题:

I'm new to C++ and in my Intro to programming design and concepts class we're now on graphics. I've been able to make programs with just FLTK's library but we have to use Bjarne's library such as GUI.h, Graph.h, Simple_window.h, Point.h. A simple program like a simple window program won't compile and gives a usual response of:

Simple_window.h:17: error: reference to ‘Window’ is ambiguous

I have also tried compiling using:

fltk-config --compile main.cpp

This yields the same results.

I have tried running the make file that Bjarne has provided with in the folder but that always comes up with errors and makes no .o files.

Note: I have also tried compiling on mac OSX and Ubuntu.

回答1:

I never used either of those libraries, but I saw that tutorials for FLTK always begin with using namespace fltk; statement, which imports all FLTK classes, including fltk::Window to the root namespace.

The library by B. Stroustrup is contained in namespace called Graph_lib and it also has a class called Window. Now, the file Simple_window.h has using namespace Graph_lib; statement at the beginning, which imports Graph_lib::Window to the root namespace. And this is where the ambiguity is coming from.

So I would suggest to omit the using statement (at least from using namespace fltk) and to use FLTK classes with full specification (e.g. fltk::Window instead of just Window). This should solve the ambiguity.

As a side note, this is nice example, why having using namespace at file level in a header file is a bad idea.

References:
http://www.fltk.org/doc-2.0/html/index.html http://www.stroustrup.com/Programming/Graphics/Simple_window.h

EDIT: I tried to compile the library containing Simple_window myself and, at least under linux, it the ambiguity seems to be between class Graph_lib::Window from the library and typedef Window from xlib as well. xlib is C library and you can't really do anything about it, so you will have to get rid of using namespace Graph_lib in Stroustup's library.

In the file Simple_window.h:

  • delete using namespace Graph_lib;
  • change Window to Graph_lib::Window
  • Button to Graph_lib::Button
  • and Address to Graph_lib::Address

Then in the file Simple_window.cpp:

  • change Address to Graph_lib::Address again
  • and reference_to<Simple_window> to Graph_lib::reference_to<Simple_window>

Then it should compile. If you have different version than the one that's on stroustrup.com, you may need to fully qualify (add Graph_lib::) more classes.



回答2:

I just had the same kind of problems (unresolved external symbols) using Simple_window.h and trying to compile a the following peace of code:

    int main(){

    // create a reference point for the window
    Point topLeft(50,50);
    // initialize a Simple_window object to size: 600x400 pixels, labeled: My window
    Simple_window myWindow(topLeft, 600, 400, "My window");
    // pass control to GUI 
    myWindow.wait_for_button();

    return 0;
    }

The solution was to add to the project (along with the main.cpp) all the respective .cpp files of the included .h files:("Graph.h", "Window.h", "Simple_window.h", "GUI.h")