【问题标题】:Stroustrup PPP Book Chapter 12: cannot convert from 'initializer list' to 'Graph_lib::Point'Stroustrup PPP 书第 12 章:无法从 'initializer list' 转换为 'Graph_lib::Point'
【发布时间】:2016-08-25 20:11:58
【问题描述】:

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

我目前正在阅读 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:\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 ==========

请问我该怎么办?老实说,在这本书的前半部分如此紧张和严肃之后,我真的很期待能玩弄图形的东西

亲切的问候,

贤三

【问题讨论】:

标签: c++ user-interface graph shared-libraries polygon


【解决方案1】:

simple_windows.h 的第 13 行可用here 看起来不是假的。所以错误必须来自其中包含的标题之一。

第一个候选者(假设)

simple_windows.h 包含一个标头GUI.h。我可以在存储库上看到here 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 章,看看是否没有分步说明/练习可以让您取消注释提供的源代码中的这一行和其他行。

编辑:建议 2 - 是的,请遵循建议 1,因为显然它可能是如 herehere 以及其他几个地方

【讨论】:

    【解决方案2】:

    首先,感谢大家的回复和cmets,很高兴听到有人帮助我,非常感谢您

    所以我基本上什么都试过了,恐怕没有任何效果,我从切换开始

    simple_windows.h

    @Christophe 链接上的版本

    然后我尝试使用 GUI.h,然后改用小写字母 Gui.h,保留原来的大写 GUI。

    当我进入 point.h 并看到你所说的所有引用的内容时,我想我终于有了它! buuuut ...也没有用:'(

    我感觉我现在调试时收到的错误消息有点短?

    我开始觉得我可能把标题放错了?我尝试将它们放在 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 ==========
    

    【讨论】:

    • 抱歉,关于 gui 大写和小写:你看过它们的内容了吗?两个版本之一包括 point.h 另一个不包括。此外,修改后的错误消息似乎表明问题从点转移到多边形(模棱两可),导致多边形未定义,然后是级联错误。关于这个歧义错误有几个关于 SO 的问题:只需在 simple_window gui 歧义上谷歌,你应该找到它。不幸的是,我只带了手机不在,所以我无法发送链接
    • 我找到了 poly 的两个定义,一个在 Graph.h 中,另一个在名为wingdi.h 的标头中,我发了截图
    • 回家。您可以查看 cplusplus.com/forum/general/121362stackoverflow.com/questions/20973419/… :两者似乎都是您的新错误条件的重复项(即 Point 不再存在初始化问题,但现在多边形出现歧义)
    【解决方案3】:

    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发送到回收站,然后在调试时遇到致命错误

    所以多边形必须在图形标题处定义错误?我可以看到多边形内部函数的一些定义,但看不到多边形本身的定义,也许我只是一个菜鸟

    我还继续将多边形更改为 Graph_library::Polygon 但没有用

    【讨论】:

      【解决方案4】:

      解决方案:

      您需要取消注释(从这些行中删除 //)您必须从 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; }
      

      【讨论】:

        猜你喜欢
        • 2012-07-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-17
        • 1970-01-01
        • 2017-04-24
        相关资源
        最近更新 更多