gpt4 book ai didi

C++ 候选构造函数不可行 : no known conversion

转载 作者:行者123 更新时间:2023-11-30 03:33:24 26 4
gpt4 key购买 nike

这是一个 PNG 类,在类文档中列出了两个构造函数,如下所示。

PNG::PNG    (   string const &  file_name   )   
Creates a PNG image by reading a file in from disk.

Parameters
file_name Name of the file to be read in to the image.

PNG::PNG ( size_t width, size_t height )
Creates a default PNG image of the desired dimensions (that is, a width x height opaque white image).

Parameters
width Width of the new image.
height Height of the new image.

我使用以下调用构造函数:

int main(){

PNG in_image=new PNG("in.png");
size_t width=in_image.width();
size_t height=in_image.height();
PNG out_image=new PNG(width,height);
}

但出现以下错误:

main.cpp:5:6: error: no viable conversion from 'PNG *' to 'PNG'
PNG in_image=new PNG("in.png");
^ ~~~~~~~~~~~~~~~~~
./png.h:62:9: note: candidate constructor not viable: no known conversion from
'PNG *' to 'const PNG &' for 1st argument; dereference the argument with *
PNG(PNG const & other);
^
./png.h:55:9: note: candidate constructor not viable: no known conversion from
'PNG *' to 'const string &' (aka 'const basic_string<char,
char_traits<char>, allocator<char> > &') for 1st argument
PNG(string const & file_name);
^
main.cpp:8:6: error: no viable conversion from 'PNG *' to 'PNG'
PNG out_image=new PNG(width,height);
^ ~~~~~~~~~~~~~~~~~~~~~
./png.h:62:9: note: candidate constructor not viable: no known conversion from
'PNG *' to 'const PNG &' for 1st argument; dereference the argument with *
PNG(PNG const & other);
^
./png.h:55:9: note: candidate constructor not viable: no known conversion from
'PNG *' to 'const string &' (aka 'const basic_string<char,
char_traits<char>, allocator<char> > &') for 1st argument
PNG(string const & file_name);

谁能给我一些提示,说明我的构造函数调用有什么问题?谢谢

最佳答案

你应该这样写:

PNG *in_image=new PNG("in.png");

size_t width=in_image->width();
size_t height=in_image->height();

PNG *out_image=new PNG(width,height);

使用 new 应该会得到一个 PNG*,即指向对象的指针。

关于C++ 候选构造函数不可行 : no known conversion,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42967297/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com