这是我的第一篇文章,所以我会尝试简短发表
我目前正在阅读Stroustrup的PPP书的第12章,我在这个YouTube视频https://www.youtube.com/watch?v=0eSOPWQ1n6U上安装了FLTK {/ 3>
已经从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:\users\kenzo\onedrive\documentos\visual studio 2015\gui.h(107): warning C4018: '<': signed/unsigned mismatch
1>c:\users\kenzo\onedrive\documentos\visual studio 2015\gui.h(112): warning C4018: '<': signed/unsigned mismatch
1>c:\users\kenzo\onedrive\documentos\visual studio 2015\gui.h(117): warning C4018: '<': signed/unsigned mismatch
1>c:\users\kenzo\onedrive\documentos\visual studio 2015\simple_window.h(13): error C2440: '<function-style-cast>': cannot convert from 'initializer list' to 'Graph_lib::Point'
1> c:\users\kenzo\onedrive\documentos\visual studio 2015\simple_window.h(13): note: No constructor could take the source type, or constructor overload resolution was ambiguous
1>c:\users\kenzo\onedrive\documentos\visual studio 2015\simple_window.h(13): error C2661: 'Graph_lib::Button::Button': no overloaded function takes 4 arguments
1>c:\users\kenzo\onedrive\documentos\visual studio 2015\projects\hello world5\hello world5\main.cpp(9): error C2440: 'initializing': cannot convert from 'initializer list' to 'Graph_lib::Point'
1> c:\users\kenzo\onedrive\documentos\visual studio 2015\projects\hello world5\hello world5\main.cpp(9): note: No constructor could take the source type, or constructor overload resolution was ambiguous
1>c:\users\kenzo\onedrive\documentos\visual studio 2015\projects\hello world5\hello world5\main.cpp(12): error C2872: 'Polygon': ambiguous symbol
1> c:\program files (x86)\windows kits\8.1\include\um\wingdi.h(4997): note: could be 'BOOL Polygon(HDC,const POINT *,int)'
1> c:\users\kenzo\onedrive\documentos\visual studio 2015\graph.h(230): note: or 'Graph_lib::Polygon'
1>c:\users\kenzo\onedrive\documentos\visual studio 2015\projects\hello world5\hello world5\main.cpp(12): error C2146: syntax error: missing ';' before identifier 'poly'
1>c:\users\kenzo\onedrive\documentos\visual studio 2015\projects\hello world5\hello world5\main.cpp(12): warning C4551: function call missing argument list
1>c:\users\kenzo\onedrive\documentos\visual studio 2015\projects\hello world5\hello world5\main.cpp(12): error C2065: 'poly': undeclared identifier
1>c:\users\kenzo\onedrive\documentos\visual studio 2015\projects\hello world5\hello world5\main.cpp(14): error C2065: 'poly': undeclared identifier
1>c:\users\kenzo\onedrive\documentos\visual studio 2015\projects\hello world5\hello world5\main.cpp(14): error C2228: left of '.add' must have class/struct/union
1> c:\users\kenzo\onedrive\documentos\visual studio 2015\projects\hello world5\hello world5\main.cpp(14): note: type is 'unknown-type'
1>c:\users\kenzo\onedrive\documentos\visual studio 2015\projects\hello world5\hello world5\main.cpp(14): error C2440: '<function-style-cast>': cannot convert from 'initializer list' to 'Graph_lib::Point'
1> c:\users\kenzo\onedrive\documentos\visual studio 2015\projects\hello world5\hello world5\main.cpp(14): note: No constructor could take the source type, or constructor overload resolution was ambiguous
1>c:\users\kenzo\onedrive\documentos\visual studio 2015\projects\hello world5\hello world5\main.cpp(15): error C2065: 'poly': undeclared identifier
1>c:\users\kenzo\onedrive\documentos\visual studio 2015\projects\hello world5\hello world5\main.cpp(15): error C2228: left of '.add' must have class/struct/union
1> c:\users\kenzo\onedrive\documentos\visual studio 2015\projects\hello world5\hello world5\main.cpp(15): note: type is 'unknown-type'
1>c:\users\kenzo\onedrive\documentos\visual studio 2015\projects\hello world5\hello world5\main.cpp(15): error C2440: '<function-style-cast>': cannot convert from 'initializer list' to 'Graph_lib::Point'
1> c:\users\kenzo\onedrive\documentos\visual studio 2015\projects\hello world5\hello world5\main.cpp(15): note: No constructor could take the source type, or constructor overload resolution was ambiguous
1>c:\users\kenzo\onedrive\documentos\visual studio 2015\projects\hello world5\hello world5\main.cpp(16): error C2065: 'poly': undeclared identifier
1>c:\users\kenzo\onedrive\documentos\visual studio 2015\projects\hello world5\hello world5\main.cpp(16): error C2228: left of '.add' must have class/struct/union
1> c:\users\kenzo\onedrive\documentos\visual studio 2015\projects\hello world5\hello world5\main.cpp(16): note: type is 'unknown-type'
1>c:\users\kenzo\onedrive\documentos\visual studio 2015\projects\hello world5\hello world5\main.cpp(16): error C2440: '<function-style-cast>': cannot convert from 'initializer list' to 'Graph_lib::Point'
1> c:\users\kenzo\onedrive\documentos\visual studio 2015\projects\hello world5\hello world5\main.cpp(16): note: No constructor could take the source type, or constructor overload resolution was ambiguous
1>c:\users\kenzo\onedrive\documentos\visual studio 2015\projects\hello world5\hello world5\main.cpp(17): error C2065: 'poly': undeclared identifier
1>c:\users\kenzo\onedrive\documentos\visual studio 2015\projects\hello world5\hello world5\main.cpp(17): error C2228: left of '.set_color' must have class/struct/union
1> c:\users\kenzo\onedrive\documentos\visual studio 2015\projects\hello world5\hello world5\main.cpp(17): note: type is 'unknown-type'
1>c:\users\kenzo\onedrive\documentos\visual studio 2015\projects\hello world5\hello world5\main.cpp(19): error C2065: 'poly': undeclared identifier
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
请问我该怎么办?说实话,我真的很期待在书的前半部分如此激烈和严肃之后搞乱图形内容
亲切的问候,贤
答案 0 :(得分:0)
可用here com.sun.management:type=DiagnosticCommand/vmNativeMemory
的第13行看起来并不虚伪。因此错误必须来自其中包含的标题之一。
第一位候选人(假设)
simple_windows.h
包含标题simple_windows.h
。我可以在存储库here上看到.B.Stroustrup提供了两个文件:GUI.h
和Gui.h
。在linux系统上,这些是两个不同的文件,因为文件名区分大小写。但是你显然正在使用Windows(Visual Studio 15),它管理文件名不敏感。所以操作系统没有区别(你肯定只有这两个文件中的一个)。
当你在第12章工作时,我建议试试GUI.h
。第一行是:
GUI.h
第二位候选人(更有可能)
我进一步研究了各种包含,发现了更可能的原因。因此,如果这不能解决问题,请查看Point.h
。其中的// This is a GUI support code to the chapters 12-16 of the book
仅使用默认构造函数定义。从x和y坐标构造一个点的构造函数被注释掉:
struct Point
这意味着在第13行的语句中,编译器突出显示为错误:
// Point(int xx, int yy) : x(xx), y(yy) { }
表达式 next_button(Point(x_max()-70,0), 70, 20, "Next", cb_next) { attach(next_button); }
确实不正确并导致您报告的错误。 取消注释此构造函数
建议: 我还没看过这本书。但是可以仔细阅读第1章到第11章,看看是否没有一步一步的指导/练习应该引导你取消注释提供的源中的这一行和其他行。
编辑:建议2 - 是的,请按照建议1,因为显然可能是here和{{3}所解释的情况在其他几个地方
答案 1 :(得分:0)
首先,感谢对所有人的回复和评论,很高兴听到有人帮我一点,非常善良
所以我基本上尝试了一切,我担心没有任何效果,我开始切换
simple_windows.h
用于@Christophe链接
的版本然后我尝试使用GUI.h
,之后使用小写Gui.h
,而保留原始的大写GUI。
当我进入point.h并看到你所说的所有引出的东西时,我终于忍受了它! buuuut ......没有工作:&#39;(
我觉得我现在调试的错误信息有点短了吗?
我开始觉得我的标题错了?我尝试将它们放在microsoft visual studio / VC的include文件夹中,同时复制它们并将它们放在头文件中打开编译器,有时也将它们直接放在项目文件夹中
在这一点上,我很抱歉是一个拖累,但我会感谢任何其他可能的解决方案
screenshot of something curious with overloads
我觉得+2重载是问题
1>------ Build started: Project: hello world2, Configuration: Debug Win32 ------
1> main.cpp
1>c:\users\kenzo\desktop\hello world2\hello world2\main.cpp(11): error C2872: 'Polygon': ambiguous symbol
1> c:\program files (x86)\windows kits\8.1\include\um\wingdi.h(4997): note: could be 'BOOL Polygon(HDC,const POINT *,int)'
1> c:\program files (x86)\microsoft visual studio 14.0\vc\include\graph.h(230): note: or 'Graph_lib::Polygon'
1>c:\users\kenzo\desktop\hello world2\hello world2\main.cpp(11): error C2146: syntax error: missing ';' before identifier 'poly'
1>c:\users\kenzo\desktop\hello world2\hello world2\main.cpp(11): warning C4551: function call missing argument list
1>c:\users\kenzo\desktop\hello world2\hello world2\main.cpp(11): error C2065: 'poly': undeclared identifier
1>c:\users\kenzo\desktop\hello world2\hello world2\main.cpp(13): error C2065: 'poly': undeclared identifier
1>c:\users\kenzo\desktop\hello world2\hello world2\main.cpp(13): error C2228: left of '.add' must have class/struct/union
1> c:\users\kenzo\desktop\hello world2\hello world2\main.cpp(13): note: type is 'unknown-type'
1>c:\users\kenzo\desktop\hello world2\hello world2\main.cpp(14): error C2065: 'poly': undeclared identifier
1>c:\users\kenzo\desktop\hello world2\hello world2\main.cpp(14): error C2228: left of '.add' must have class/struct/union
1> c:\users\kenzo\desktop\hello world2\hello world2\main.cpp(14): note: type is 'unknown-type'
1>c:\users\kenzo\desktop\hello world2\hello world2\main.cpp(15): error C2065: 'poly': undeclared identifier
1>c:\users\kenzo\desktop\hello world2\hello world2\main.cpp(15): error C2228: left of '.add' must have class/struct/union
1> c:\users\kenzo\desktop\hello world2\hello world2\main.cpp(15): note: type is 'unknown-type'
1>c:\users\kenzo\desktop\hello world2\hello world2\main.cpp(16): error C2065: 'poly': undeclared identifier
1>c:\users\kenzo\desktop\hello world2\hello world2\main.cpp(16): error C2228: left of '.set_color' must have class/struct/union
1> c:\users\kenzo\desktop\hello world2\hello world2\main.cpp(16): note: type is 'unknown-type'
1>c:\users\kenzo\desktop\hello world2\hello world2\main.cpp(18): error C2065: 'poly': undeclared identifier
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
答案 2 :(得分:0)
I had no idea i had this header somewhere, must have been instaled from the fltk.org zip?
this is from the header downloaded from Stroustrup
我去了,并将wingdi.h发送到回收站,然后我在调试时遇到致命错误
所以必须在图表标题中严格定义poly?我可以看到多边形内部函数的一些定义,但不是多边形本身的定义,也许我只是一个菜鸟
我还继续将多边形更改为Graph_library :: Polygon但是没有工作
答案 3 :(得分:0)
解决方案:
您需要取消注释(必须从这些行中删除//),这些行必须已从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; }