【问题标题】:C++ Overload: [Error] no match for 'operator=' (operand types are 'String' and 'String') [duplicate]C ++重载:[错误]'operator ='不匹配(操作数类型是'String'和'String')[重复]
【发布时间】:2018-01-04 09:31:28
【问题描述】:

我正在通过学​​习 Visual C++ 教科书来学习 C++。
当我想重载 operator+ 时,我用于重载 operator= 的代码出错了。

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

这很奇怪,因为单独使用 operator+ 或 operator= 时效果很好。

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

(A_string+B_string).show_string();

这是错误

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&'

我认为我可以使用 String 作为参数 String&,这在书中有所讲述。
所以我把operator=的参数改成String,就可以了。

String& operator=(String&);

String& operator=(String);

现在我很困惑何时只使用引用或字符串。

【问题讨论】:

  • 您以“In the book”开头的评论毫无意义,代码相同
  • 您的对象在运行时会出现异常,因为它没有正确的复制构造函数
  • 应该是String&amp; String::operator=(const String&amp;)。寻找骗子。
  • operator+ 也应该采用(String const&amp;)
  • 临时不能绑定到非 const 左值引用。

标签: c++ class reference operator-overloading assignment-operator


【解决方案1】:

在此声明中

C_string = A_string + B_string;

作为执行操作符的结果,创建了一个String 类型的临时对象

String operator+(String&);      

您不能将非常量左值引用绑定到临时对象。

重写/添加赋值运算符,如

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

或添加移动赋值运算符。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-09
    • 2023-04-09
    • 1970-01-01
    相关资源
    最近更新 更多