![](/img/trans.png)
[英]C++ lambda conversion — why is candidate constructor not viable: no known conversion from lambda to std::function
[英]C++ Candidate constructor not viable: no known conversion
这是一个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*
即指向对象的指针。
您应该这样写:
PNG in_image("in.png");
size_t width = in_image.width();
size_t height = in_image.height();
PNG out_image(width, height);
C ++不是Java-您可以使用new
来动态分配对象,但如果不这样做,则不要使用它。 除非确实需要,否则不应该使用new
。
运营商新的类型X
将分配内存为X
,并返回它的地址是型X*
。
因此,您应该将其收集到一个指针变量中:
PNG* in_image = new PNG("in.png"); // in_image points to the newly created PNG object
size_t width = in_image.width();
size_t height = in_image.height();
PNG* out_image = new PNG(width,height); // another object is created and out_image points to it
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.