【问题标题】:C++ Candidate constructor not viable: no known conversionC++ 候选构造函数不可行:没有已知的转换
【发布时间】:2017-03-23 04:32:23
【问题描述】:

这是一个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);

任何人都可以就我的构造函数调用有什么问题给出一些提示吗? 谢谢

【问题讨论】:

    标签: c++ copy-constructor


    【解决方案1】:

    你应该这样写:

    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* 即指向对象的指针。

    【讨论】:

    • height() 后面缺少分号
    • 完成。谢谢@ABusyProgrammer
    【解决方案2】:

    你应该这样写:

    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

    【讨论】:

      【解决方案3】:

      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
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-10-08
        • 1970-01-01
        • 2022-12-04
        • 2021-04-21
        • 1970-01-01
        • 2016-01-10
        • 2021-12-31
        • 1970-01-01
        相关资源
        最近更新 更多