Stroustrup的Simple_window.h [英] Stroustrup's Simple_window.h

查看:362
本文介绍了Stroustrup的Simple_window.h的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图从Stroustrup的原理和实践... C ++中获得图形示例,但无济于事。我已经安装了fltk的东西,并知道这是工作正常,因为我设法得到一个窗口显示使用在他的书的附录中建议的程序:

  #include< FL / Fl.H> 
#include< FL / Fl_Box.H>
#include< FL / Fl_Window.H>

int main(){

Fl_Window窗口(200,200,title here);
Fl_Box box(0,0,200,200,Hey,hello wrld);
window.show();
return Fl :: run();但是,尝试我自己的使用他的Simple_window.h(可以在他的发现上找到他的。)(
}



<站点)给出引用''窗口'是不明确的,因为它已经在usr / include / X11 / Xh。所以我试着指定命名空间到相关的命名空间:

  struct Simple_window:Graph_lib :: Window {命名空间
Simple_window(Point xy,int w,int h,const string& title);

bool wait_for_button(); //简单事件循环




但是这给我一个更多的错误我不明白:

  $ clear; g ++ -Wno-deprecated window.cpp -o holz 
/tmp/ccIFivNg.o:在函数main中:
window.cpp :(。text + 0x64):未定义引用`Simple_window :: Simple_window(Point,int,int,String const&)'
/tmp/ccIFivNg.o:在函数`Graph_lib :: Window ::〜Window()'中:
window.cpp :(。 ._ZN9Graph_lib6WindowD2Ev [_ZN9Graph_lib6WindowD5Ev] + 0x14):未定义引用`vtable for Graph_lib :: Window'



我认为掌握图形将是一条漫长而艰难的道路--_

解决方案

对于同样困境中的任何人,我离开这里,我做了最终能够编译并获得第一个程序的窗口与FLTK的第12.3节的Stroustrup的书编程:原理和实践使用C + ,第2版。



在Kubuntu 14.04上安装FLTK后使用

  $ sudo apt install libfltk1.3-dev 



我可以编译附录D中的示例程序使用

  $ fltk-config --compile fltkTest.cpp 

感谢这篇文章,我可以看到我最终能够与第12章的第一个例子在轨道上。比较cwivagg和Nathan的命令与生成的命令与fltk-config,我以此命令结束

  $ clang ++ -I / usr / include / cairo -I / usr / include /glib-2.0 -I / usr / lib / x86_64-linux-gnu / glib-2.0 / include -I / usr / include / pixman-1 -I / usr / include / freetype2 -I / usr / include / libpng12 -I / usr / include / freetype2 -I / usr / include / cairo -I / usr / include / glib-2.0 -I / usr / lib / x86_64-linux-gnu / glib-2.0 / include -I / usr / include / pixman -1 -i / usr / include / freetype2 -I / usr / include / libpng12 -g -O2 -D_LARGEFILE_SOURCE -D_LARGEFILE64_SOURCE -D_THREAD_SAFE -D_REENTRANT -Wl,-Bsymbolic-functions -lfltk_images -lfltk -lX11 -std = c ++ 11 -o's12_3_first''s12_3_first.cpp'Simple_window.cpp Graph.cpp GUI.cpp Window.cpp 

我不得不添加-lfltk_images和-std = c ++ 11



然而,现在我不得不处理编译器给我的错误。为了得到一个工作程序,我不得不对Stroustrup对 http:// www .stroustrup.com / Programming / PPP2code /


  1. 我取消注释Graph.h上的std_lib_facilities.h

  2. 为了解决Window的模糊性,我需要在Simple_window.h的第9行上指定Graph_lib :: Window。

  3. 在第107行和第113行使用i <

  4. Graph.h第159行使用fl_color(),但是编译器说它应该是Fl_Color

  5. 我需要取消注释Point中的Point的构造函数。

  6. 在Simple_window.h的Simple_window.cpp上有几个重定义
    在Simple_window.cpp我注释了构造函数的定义,
    cb_next和wait_for_button(这与
    Simple_window.h中的不一样)。在Simple_window.h我注释掉了
    的定义wait_for_button和next。顺便说一句,wait_for_button在
    中不起作用。

  7. 在GUI.cpp中,有一个重新定义Menu的构造函数。

  8. 我更改了第12.3节的示例的最后一行
    from
    win.wait_for_button;
    to
    Fl :: run();
    ,我从附录D的示例中获取,因为否则窗口会
    不以关闭按钮关闭。

对于所有这些更改,我终于有窗口,因为它应该是,窗口关闭与下一个按钮或关闭按钮的所述窗口(与wait_for_button我需要结束程序从Konsole与Ctrl-c后我试图用窗口的关闭按钮关闭它。)



我希望下一个人不必花所有的时间,我有。



编辑:好吧,检查我的系统和编译命令,我意识到有几个地毯重复...而且他们实际上不存在在我的Kubuntu系统。所以,我必须写下我的答案,我终于要让窗口工作:



要获得一个对象文件:

  $ clang ++ -O2 -D_LARGEFILE_SOURCE -D_LARGEFILE64_SOURCE -D_THREAD_SAFE -D_REENTRANT -g -std = c ++ 11 -c Simple_window.cpp 



获得我们想要的第一个程序

 %clang ++ -O2 -D_LARGEFILE_SOURCE -D_LARGEFILE64_SOURCE -D_THREAD_SAFE -D_REENTRANT -Wl,-Bsymbolic-functions -lfltk_images -lfltk -lX11 -g -std = c ++ 11 Simple_window.o Graph.o GUI.o Window.o -o z3 s12_3_first.cpp 

这些是一个地狱很容易(我几乎可以写它们每次我需要它们)


I am trying to get the graphics examples to work from Stroustrup's Principles and Practices ...C++, to no avail (yet). I have installed the fltk stuff, and know that is working fine as I managed to get a window to display using with a program suggested in the appendix of his book:

#include <FL/Fl.H>
#include <FL/Fl_Box.H>
#include <FL/Fl_Window.H>

int main(){

    Fl_Window window(200,200, "title here");
    Fl_Box box(0,0,200,200,"Hey, hello wrld");
    window.show();
    return Fl::run();
}

However, trying my own using his Simple_window.h (can be found on his site) gives "reference to ‘Window’ is ambiguous", since it's already at usr/include/X11/X.h . So I tried specifying the namespace to the relevant one:

struct Simple_window : Graph_lib::Window {  //Changed Window to inc. namespace
    Simple_window(Point xy, int w, int h, const string& title );

    bool wait_for_button(); // simple event loop

.
.
.

But this gives me a bunch more errors I don't understand:

$ clear; g++ -Wno-deprecated window.cpp -o holz
    /tmp/ccIFivNg.o: In function `main':
    window.cpp:(.text+0x64): undefined reference to `Simple_window::Simple_window(Point, int, int, String const&)'
    /tmp/ccIFivNg.o: In function `Graph_lib::Window::~Window()':
    window.cpp:(.text._ZN9Graph_lib6WindowD2Ev[_ZN9Graph_lib6WindowD5Ev]+0x14): undefined reference to `vtable for Graph_lib::Window'

etc.

I feel mastering graphics is going to be a long and rocky road -_-

解决方案

To anyone in the same predicament, I leave here what I did to finally be able to compile and get the window of the first program with FLTK on section 12.3 of Stroustrup's book "Programming: Principles and Practice using C++, 2nd Edition".

After installing FLTK on Kubuntu 14.04 with

$ sudo apt install libfltk1.3-dev

I could compile the example program on Appendix D with the use of

$ fltk-config --compile fltkTest.cpp

Thanks to this post, I could see how I could finally get it on track with the first example of chapter 12. Comparing the command of cwivagg and Nathan with the command generated with fltk-config, I ended with this command

$ clang++ -I/usr/include/cairo -I/usr/include/glib-2.0 -I/usr/lib/x86_64-linux-gnu/glib-2.0/include -I/usr/include/pixman-1 -I/usr/include/freetype2 -I/usr/include/libpng12 -I/usr/include/freetype2 -I/usr/include/cairo -I/usr/include/glib-2.0 -I/usr/lib/x86_64-linux-gnu/glib-2.0/include -I/usr/include/pixman-1 -I/usr/include/freetype2 -I/usr/include/libpng12 -g -O2 -D_LARGEFILE_SOURCE -D_LARGEFILE64_SOURCE -D_THREAD_SAFE -D_REENTRANT -Wl,-Bsymbolic-functions -lfltk_images -lfltk -lX11 -std=c++11 -o 's12_3_first' 's12_3_first.cpp' Simple_window.cpp Graph.cpp GUI.cpp Window.cpp

I had to add -lfltk_images and -std=c++11

However, now I had to deal with the errors that the compiler gave me. To get a working program, I had to do several changes to the sources that Stroustrup gave on http://www.stroustrup.com/Programming/PPP2code/

  1. I uncommented std_lib_facilities.h on Graph.h
  2. To resolve the ambiguity of Window, I needed to specify Graph_lib::Window on line 9 of Simple_window.h
  3. std_lib_facilities.h on lines 107 and 113 uses a i<0 comparison when i is unsigned (but these are just warnings).
  4. Graph.h line 159 uses fl_color() but the compiler says it should be Fl_Color
  5. I needed to uncomment the constructors for Point in Point.h
  6. There are several redefinitions on Simple_window.cpp of Simple_window.h On Simple_window.cpp I commented out the definitions for the constructor, cb_next and wait_for_button (which is not the same as the one on Simple_window.h). On Simple_window.h I commented out the definitions of wait_for_button and next. By the way, wait_for_button does not work in either form.
  7. In GUI.cpp there is another redefinition for the constructor of Menu. I commented it out.
  8. I changed the last line of the example of section 12.3 from win.wait_for_button; to Fl::run(); which I took from the example on Appendix D, because otherwise the window does not close with its close button.

With all these changes I finally have the window as it should be, and the window close either with the Next button or the close button of the said window (with wait_for_button I needed to end the program from Konsole with Ctrl-c after I tried to close it with the close button of the window).

I hope the next person do not have to spend all the time I had to.

Edit: Well, checking at my system and the compiling command, I realized that there are several carpets repeated... and that they actually don't exist in my Kubuntu system. So, I have to write down in my answer what I finally do to get the window working:

To get an object file:

$ clang++ -O2 -D_LARGEFILE_SOURCE -D_LARGEFILE64_SOURCE -D_THREAD_SAFE -D_REENTRANT -g -std=c++11 -c  Simple_window.cpp

To get the first program that we wanted

% clang++ -O2 -D_LARGEFILE_SOURCE -D_LARGEFILE64_SOURCE -D_THREAD_SAFE -D_REENTRANT -Wl,-Bsymbolic-functions -lfltk_images -lfltk -lX11 -g -std=c++11 Simple_window.o Graph.o GUI.o Window.o -o z3 s12_3_first.cpp

These are a hell lot easier (I almost can write them every time I need them)

这篇关于Stroustrup的Simple_window.h的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆