title: C++ Overload: [Error] no match for 'operator=' (operand types are 'String' and 'String')
body: <p>I am learning C++ by studying Visual C++ textbook.<br>
  When I want to overload operator+, the code I've used for overloading operator= went wrong.<br></p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;string.h&gt;
  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&amp; operator=(String&amp;);     //In the book it used //String &amp; operator=(String&amp;) but it went wrong
                                          //String object which returned by + only got value, not an initiated object
          String operator+(String&amp;);      //Opreator +
          void show_string(){
              cout &lt;&lt; "String: " &lt;&lt; string &lt;&lt; " \tString Address: " &lt;&lt; (void*)string &lt;&lt; " \tLength: " &lt;&lt; len &lt;&lt; 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&amp; String::operator=(String&amp; str_ref){     //Overloading operator =
  //The function get reference and return itself
      delete [] string;
      cout &lt;&lt; "Overloading Operator =...\n";
      len = str_ref.len;
      string = new char[len+1];
      strcpy(string,str_ref.string);
      return *this;
  }
  
  String String::operator+(String&amp; str){
      cout &lt;&lt; "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 &lt;&lt; "Show (A_string+B_string)...\n";
      (A_string+B_string).show_string();
      C_string = A_string + B_string;
      cout &lt;&lt; "Show C_string...\n";
      C_string.show_string();
  return 0;
  }
  </code></pre>
  
  <p>It's strange because it did well when only use operator+ or operator= individually.</p>
  
  <pre><code>String A_string("Apple");
  String B_string;
  B_string = A_string;
  
  (A_string+B_string).show_string();
  </code></pre>
  
  <p>Here's the error</p>
  
  <pre><code>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&amp; String::operator=(String&amp;)
   String&amp; String::operator=(String&amp; str_ref){  \\Overloading operator =
           ^
  note:   no known conversion for argument 1 from 'String' to 'String&amp;'
  </code></pre>
  
  <p>I thought I can use String for argument String&amp;, which was told in the book.<br>
  So I changed argument of operator= to String, and it works.</p>
  
  <pre><code>String&amp; operator=(String&amp;);
  </code></pre>
  
  <p>to</p>
  
  <pre><code>String&amp; operator=(String);
  </code></pre>
  
  <p>Now I am confused when to use reference or String only.</p>