Stroustrup PPP Book第12章:无法转换'& # 39; Graph_lib::点# 39;



这是我的第一篇文章,所以我会尽量简短一些

我现在正在读Stroustrup的PPP书的第12章,我安装了FLTK,就像他们在这个youtube视频https://www.youtube.com/watch?v=0eSOPWQ1n6U上做的那样

已经从Stroustrup的网站下载了头文件,我正试图用GUI做第一个例子练习,在那里你做了一个红色三角形

代码:

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

我已经尝试了几天不同的事情,但我一直被撞到墙上,这是我从代码中得到的错误,现在安装了我所知道的

1>------ Build started: Project: hello world5, Configuration: Debug Win32 ------
1>  main.cpp
1>c:userskenzoonedrivedocumentosvisual studio 2015gui.h(107): warning C4018: '<': signed/unsigned mismatch
1>c:userskenzoonedrivedocumentosvisual studio 2015gui.h(112): warning C4018: '<': signed/unsigned mismatch
1>c:userskenzoonedrivedocumentosvisual studio 2015gui.h(117): warning C4018: '<': signed/unsigned mismatch
1>c:userskenzoonedrivedocumentosvisual studio 2015simple_window.h(13): error C2440: '<function-style-cast>': cannot convert from 'initializer list' to 'Graph_lib::Point'
1>  c:userskenzoonedrivedocumentosvisual studio 2015simple_window.h(13): note: No constructor could take the source type, or constructor overload resolution was ambiguous
1>c:userskenzoonedrivedocumentosvisual studio 2015simple_window.h(13): error C2661: 'Graph_lib::Button::Button': no overloaded function takes 4 arguments
1>c:userskenzoonedrivedocumentosvisual studio 2015projectshello world5hello world5main.cpp(9): error C2440: 'initializing': cannot convert from 'initializer list' to 'Graph_lib::Point'
1>  c:userskenzoonedrivedocumentosvisual studio 2015projectshello world5hello world5main.cpp(9): note: No constructor could take the source type, or constructor overload resolution was ambiguous
1>c:userskenzoonedrivedocumentosvisual studio 2015projectshello world5hello world5main.cpp(12): error C2872: 'Polygon': ambiguous symbol
1>  c:program files (x86)windows kits8.1includeumwingdi.h(4997): note: could be 'BOOL Polygon(HDC,const POINT *,int)'
1>  c:userskenzoonedrivedocumentosvisual studio 2015graph.h(230): note: or       'Graph_lib::Polygon'
1>c:userskenzoonedrivedocumentosvisual studio 2015projectshello world5hello world5main.cpp(12): error C2146: syntax error: missing ';' before identifier 'poly'
1>c:userskenzoonedrivedocumentosvisual studio 2015projectshello world5hello world5main.cpp(12): warning C4551: function call missing argument list
1>c:userskenzoonedrivedocumentosvisual studio 2015projectshello world5hello world5main.cpp(12): error C2065: 'poly': undeclared identifier
1>c:userskenzoonedrivedocumentosvisual studio 2015projectshello world5hello world5main.cpp(14): error C2065: 'poly': undeclared identifier
1>c:userskenzoonedrivedocumentosvisual studio 2015projectshello world5hello world5main.cpp(14): error C2228: left of '.add' must have class/struct/union
1>  c:userskenzoonedrivedocumentosvisual studio 2015projectshello world5hello world5main.cpp(14): note: type is 'unknown-type'
1>c:userskenzoonedrivedocumentosvisual studio 2015projectshello world5hello world5main.cpp(14): error C2440: '<function-style-cast>': cannot convert from 'initializer list' to 'Graph_lib::Point'
1>  c:userskenzoonedrivedocumentosvisual studio 2015projectshello world5hello world5main.cpp(14): note: No constructor could take the source type, or constructor overload resolution was ambiguous
1>c:userskenzoonedrivedocumentosvisual studio 2015projectshello world5hello world5main.cpp(15): error C2065: 'poly': undeclared identifier
1>c:userskenzoonedrivedocumentosvisual studio 2015projectshello world5hello world5main.cpp(15): error C2228: left of '.add' must have class/struct/union
1>  c:userskenzoonedrivedocumentosvisual studio 2015projectshello world5hello world5main.cpp(15): note: type is 'unknown-type'
1>c:userskenzoonedrivedocumentosvisual studio 2015projectshello world5hello world5main.cpp(15): error C2440: '<function-style-cast>': cannot convert from 'initializer list' to 'Graph_lib::Point'
1>  c:userskenzoonedrivedocumentosvisual studio 2015projectshello world5hello world5main.cpp(15): note: No constructor could take the source type, or constructor overload resolution was ambiguous
1>c:userskenzoonedrivedocumentosvisual studio 2015projectshello world5hello world5main.cpp(16): error C2065: 'poly': undeclared identifier
1>c:userskenzoonedrivedocumentosvisual studio 2015projectshello world5hello world5main.cpp(16): error C2228: left of '.add' must have class/struct/union
1>  c:userskenzoonedrivedocumentosvisual studio 2015projectshello world5hello world5main.cpp(16): note: type is 'unknown-type'
1>c:userskenzoonedrivedocumentosvisual studio 2015projectshello world5hello world5main.cpp(16): error C2440: '<function-style-cast>': cannot convert from 'initializer list' to 'Graph_lib::Point'
1>  c:userskenzoonedrivedocumentosvisual studio 2015projectshello world5hello world5main.cpp(16): note: No constructor could take the source type, or constructor overload resolution was ambiguous
1>c:userskenzoonedrivedocumentosvisual studio 2015projectshello world5hello world5main.cpp(17): error C2065: 'poly': undeclared identifier
1>c:userskenzoonedrivedocumentosvisual studio 2015projectshello world5hello world5main.cpp(17): error C2228: left of '.set_color' must have class/struct/union
1>  c:userskenzoonedrivedocumentosvisual studio 2015projectshello world5hello world5main.cpp(17): note: type is 'unknown-type'
1>c:userskenzoonedrivedocumentosvisual studio 2015projectshello world5hello world5main.cpp(19): error C2065: 'poly': undeclared identifier
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

请问我该怎么办?老实说,在这本书的前半部分如此紧张和严肃之后,我真的很期待在图像方面做些改变。

亲切的问候,

高田贤三

这里可用的simple_windows.h的第13行看起来并不假。因此,错误必须来自其中包含的标头之一。

第一个候选者(假设)

simple_windows.h包含一个报头GUI.h。B.Stroustrup提供了两个文件:Gui.hGUI.h。在linux系统上,这是两个不同的文件,因为文件名区分大小写。但你显然是在windows (Visual Studio 15)下工作,它管理文件名不敏感。因此,操作系统没有区别(当然,您只有这两个文件中的一个)。

当你正在学习第12章时,我建议你尝试GUI.h。第一行是:

// This is a GUI support code to the chapters 12-16 of the book

第二个候选(更可能)

我进一步研究了各种包括,发现了一个更可能的原因。因此,如果这不能解决问题,那么看看Point.h。其中的struct Point仅用默认构造函数定义。从x和y坐标构造一个点的构造函数被注释掉:

 // Point(int xx, int yy) : x(xx), y(yy) { }

这意味着在第13行的语句中,编译器会突出显示为错误:

 next_button(Point(x_max()-70,0), 70, 20, "Next", cb_next) { attach(next_button); }

表达式Point(x_max()-70,0)确实不正确,并导致您报告的错误。取消这个构造函数的注释

建议:我还没有读过这本书。但是你可以重新仔细阅读第1章到第11章,看看是否有一步一步的指导/练习可以让你在提供的源代码中取消这一行和其他行的注释。

编辑:advice 2 -是的,遵循advice 1,因为显然它可能是这里和这里以及其他几个地方解释的情况

首先,感谢所有的回复和评论,很高兴听到有人帮助我,非常感谢你

所以我基本上尝试了所有的方法,但我担心没有任何效果,我从切换

开始

simple_windows.h

查看@Christophe的版本link

然后我试着用GUI.h,后来用小写的Gui.h代替,留下原来的大写GUI。

当我打开point.h,看到所有的东西都像你说的那样被引用出来时,我以为我终于明白了!buuuut……'(

)

我有一种感觉,我得到的错误信息调试现在有点短吗?

我开始觉得也许我把标题错了?我试着把它们放在包含文件夹从microsoft visual studio/VC,也复制它们,并将它们放在头文件与编译器打开,有时也把它们直接放在项目文件夹

在这一点上,我很抱歉打扰你,但我将感谢任何其他可能的解决方案

重载的一些奇怪的东西的截图

我感觉+2过载是问题

1>------ Build started: Project: hello world2, Configuration: Debug Win32 ------
1>  main.cpp
1>c:userskenzodesktophello world2hello world2main.cpp(11): error C2872: 'Polygon': ambiguous symbol
1>  c:program files (x86)windows kits8.1includeumwingdi.h(4997): note: could be 'BOOL Polygon(HDC,const POINT *,int)'
1>  c:program files (x86)microsoft visual studio 14.0vcincludegraph.h(230): note: or       'Graph_lib::Polygon'
1>c:userskenzodesktophello world2hello world2main.cpp(11): error C2146: syntax error: missing ';' before identifier 'poly'
1>c:userskenzodesktophello world2hello world2main.cpp(11): warning C4551: function call missing argument list
1>c:userskenzodesktophello world2hello world2main.cpp(11): error C2065: 'poly': undeclared identifier
1>c:userskenzodesktophello world2hello world2main.cpp(13): error C2065: 'poly': undeclared identifier
1>c:userskenzodesktophello world2hello world2main.cpp(13): error C2228: left of '.add' must have class/struct/union
1>  c:userskenzodesktophello world2hello world2main.cpp(13): note: type is 'unknown-type'
1>c:userskenzodesktophello world2hello world2main.cpp(14): error C2065: 'poly': undeclared identifier
1>c:userskenzodesktophello world2hello world2main.cpp(14): error C2228: left of '.add' must have class/struct/union
1>  c:userskenzodesktophello world2hello world2main.cpp(14): note: type is 'unknown-type'
1>c:userskenzodesktophello world2hello world2main.cpp(15): error C2065: 'poly': undeclared identifier
1>c:userskenzodesktophello world2hello world2main.cpp(15): error C2228: left of '.add' must have class/struct/union
1>  c:userskenzodesktophello world2hello world2main.cpp(15): note: type is 'unknown-type'
1>c:userskenzodesktophello world2hello world2main.cpp(16): error C2065: 'poly': undeclared identifier
1>c:userskenzodesktophello world2hello world2main.cpp(16): error C2228: left of '.set_color' must have class/struct/union
1>  c:userskenzodesktophello world2hello world2main.cpp(16): note: type is 'unknown-type'
1>c:userskenzodesktophello world2hello world2main.cpp(18): error C2065: 'poly': undeclared identifier
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

我不知道我在某个地方有这个头,一定是从fltk.org zip安装的?

这是从Stroustrup下载的头文件

我去把wingdi.h发送到回收站,然后在调试时我得到了一个致命的错误

所以多边形在图头可能定义得很糟糕?我可以看到多边形内部函数的一些定义但没有多边形本身的定义,也许我只是一个新手

我还继续并将polygon更改为Graph_library:: polygon,但没有工作

解决方案:

您需要取消注释(从这些行中删除//)来自您必须从http://www.stroustrup.com/Programming/PPP2code/

下载的"Point.h"文件中的以下行
Point(int xx, int yy) : x(xx), y(yy) { }
Point() :x(0), y(0) { }
Point& operator+=(Point d) { x+=d.x; y+=d.y; return *this; }

相关内容

  • 没有找到相关文章

最新更新