1

J'apprends le C++ en étudiant le manuel Visual C++.
Lorsque je veux surcharger l'opérateur +, le code que j'ai utilisé pour surcharger l'opérateur = s'est mal passé.
Surcharge C++: [Erreur] aucune correspondance pour 'opérateur =' (les types d'opérande sont 'Chaîne' et 'Chaîne')

#include <iostream> 
#include <string.h> 
using namespace std; 
//This demo shows how default operator may cause conflict, so we use overloaded operator instead 
class String{ 
    private: 
     char* string; 
     int len; 
    public: 
     String(const char*); 
     String(); 
     ~String(){delete [] string;} 
     String& operator=(String&);  //In the book it used //String & operator=(String&) but it went wrong 
             //String object which returned by + only got value, not an initiated object 
     String operator+(String&);  //Opreator + 
     void show_string(){ 
      cout << "String: " << string << " \tString Address: " << (void*)string << " \tLength: " << len << endl; 
     } 
}; 

String::String():len(0){  //Constructor with no argument 
    string = new char[len+1]; 
    string[0] = '\0'; 
} 

String::String(const char* i_string):len(strlen(i_string)){ //Constructor 
    string = new char[len+1]; 
    strcpy(string,i_string); 
} 

String& String::operator=(String& str_ref){  //Overloading operator = 
//The function get reference and return itself 
    delete [] string; 
    cout << "Overloading Operator =...\n"; 
    len = str_ref.len; 
    string = new char[len+1]; 
    strcpy(string,str_ref.string); 
    return *this; 
} 

String String::operator+(String& str){ 
    cout << "Overloading Operator +...\n"; 
    char* strbuf = new char[len+str.len+1]; 
    strcpy(strbuf,string); 
    strcat(strbuf,str.string); 
    String retstr(strbuf); 
    delete [] strbuf; 
    return retstr;  //call by value coz we made a new String 
} 

int main(){ 
    String A_string("My "); 
    String B_string("string"),C_string; 
    cout << "Show (A_string+B_string)...\n"; 
    (A_string+B_string).show_string(); 
    C_string = A_string + B_string; 
    cout << "Show C_string...\n"; 
    C_string.show_string(); 
return 0; 
} 

Il est étrange parce qu'il a fait bien quand utiliser seul opérateur + ou opérateur = individuellement.

String A_string("Apple"); 
String B_string; 
B_string = A_string; 

(A_string+B_string).show_string(); 

est ici l'erreur

In function 'int main()': 
56:11: error: no match for 'operator=' (operand types are 'String' and 'String') 
    C_string = A_string + B_string; 
     ^

note: candidate is: 
note: String& String::operator=(String&) 
String& String::operator=(String& str_ref){ \\Overloading operator = 
     ^
note: no known conversion for argument 1 from 'String' to 'String&' 

Je pensais que je peux utiliser pour cordes Argument de chaîne &, qui a été dit dans le livre.
J'ai donc changé l'argument de operator = en String, et cela fonctionne.

String& operator=(String&); 

à

String& operator=(String); 

Maintenant, je suis confus quand utiliser la référence ou une chaîne uniquement.

+0

Votre commentaire à partir « Dans le livre » n'a pas de sens, qui est le même code –

+0

Votre objet se conduisent mal à l'exécution, car il ne dispose pas d'une bonne copie constructeur –

+0

doit-il être l'opérateur 'String & String :: = (const String &) '. Vous cherchez une dupe. –

Répondre

0

Dans cette déclaration

C_string = A_string + B_string; 

on crée un objet temporaire du type String comme résultat de l'exécution de l'opérateur

String operator+(String&);  

Vous ne pouvez pas lier une référence lvalue non constante à un temporaire objet.

Soit récrire/ajouter l'opérateur d'affectation comme

String& operator=(const String&);  
        ^^^^ 

ou ajouter un opérateur d'affectation de déplacement.

String& operator=(String &&);  
          ^^