2017-03-23 6 views
1

Dies ist eine PNG-Klasse mit zwei Konstruktoren wie unten im Klassendokument aufgeführt.C++ - Kandidat Konstruktor nicht lebensfähig: keine bekannte Konvertierung

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. 

Ich verwende die folgenden Konstruktoren aufrufen:

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

aber bekam Fehler unter:

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

Könnte jemand ein paar Hinweise geben, was ist falsch mit meinem Konstruktor aufrufen? Thx

Antwort

1

Sie sollten es so schreiben:

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

neue Verwendung sollten Sie einen PNG* das heißt einen Zeiger auf das Objekt erhalten.

+0

Sie ein Semikolon nach 'Höhe() fehlt' –

+0

Fertig. Vielen Dank @ABusyProgrammer – Rishi

1

Sie sollten es so schreiben:

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

C++ ist nicht Java - Sie new dynamisch ein Objekt zuweisen können, aber Sie es nicht verwenden, wenn Sie das nicht tun, sind. Sie sollten new nicht verwenden, außer Sie brauchen es wirklich.

0

Operator neu für Typ X wird Speicher für X reservieren und seine Adresse zurückgeben, die vom Typ ist.
So sollten Sie es in einer Zeigervariable sammeln:

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 
Verwandte Themen