简体   繁体   中英

C++ Candidate constructor not viable: no known conversion

This is a PNG class with two constructors listed as below in class document.

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.

I use the following to invoke constructors:

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);
}

But got errors below:

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);

Could anyone give some hints on what is wrong with my constructor invoke? Thx

You should write it like this :

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);

Using new should get you a PNG* ie a pointer to the object.

You should write it like this:

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

C++ is not Java - you can use new to dynamically allocate an object, but you don't use it when you're not doing that. You shouldn't use new unless you actually need it.

operator new for type X will allocate memory for X and return it's address which is of type X* .
So you should collect it in a pointer variable:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM