C++ 候选构造函数不可行:没有已知的转换
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
运算符new将为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
相关文章:
- 防止主数据类型C++的隐式转换
- 模板参数替换失败,并且未完成隐式转换
- 努力将整数转换为链表。不知道我在这里做错了什么
- HEX值到wchar_t字符(UTF-8)的转换
- lambda参数转换为constexpr技巧,然后获取带链接的数组
- 将 Qvector<uint8_t> 转换为 QString
- 如何在cuSparse中使用cusparseXcoo2csr从coo转换为csc
- 有关插入适配器的错误。[错误]请求从 'back_insert_iterator<vector<>>' 类型转换为非标量类型
- 在c++中使用nlohmann从类到json的转换
- 从"int*"强制转换为"unsigned int"会丢失精度错误
- 将Integer转换为4字节的unsined字符矢量(按大端字节顺序)
- 处理小于cpu数据总线的数据类型.(c++转换为机器代码)
- 如何使用OpenCV将RBG图像转换为HSV,并将H、S和V值保存为C++中的3个独立图像
- 复制列表初始化的隐式转换的等级是多少
- 正在将指针转换为范围
- 如何防止 c++ 在从浮点型转换为双精度型(不适用于 IO)时添加额外的小数?
- 候选函数不可行:没有从 std::vector<derived> 到 std::vector <base>的已知转换
- C++ - 候选函数不可行:没有从"结构"到"结构(&)"的已知转换
- C++ 候选构造函数不可行:没有已知的转换
- 错误:候选构造函数(隐式移动构造函数)不可行:没有已知的转换