求大佬帮我分析下呗…
ICE丶L 2019-04-16 11:47:53 编译时会出现:stringOverload.cc:28:5: error: no match for ‘operator<<’ (operand types are ‘std::ostream {aka std::basic_ostream<char>}’ and ‘void’)
cout<<s1.display()<<endl;
#include<string.h>
#include<iostream>
using namespace std;
class String{
public:
String(string str)
:_str(str)
{
cout<<"Sting()"<<endl;
}
String operator=(const String &str2){
return String(_str=str2._str);}
void display()const{
cout<<_str<<endl;
}
private:
string _str;
};
int main(){
String s1("hello");
String s2("world");
cout<<"before overload"<<endl;
cout<<s1.display()<<endl;
//cout<<s2.display()<<endl;
s1=s2;
cout<<"after overload"<<endl;
//cout<<s1.display()<<endl;
//cout<<s2.display()<<endl;
}