定义一个String类重载+与<<
#include<string>
#include<iostream>
using namespace std;
class String
{
char str[80];
public:
String(){strcpy(str," ");}
String(char s[]){strcpy(str,s);}
void display()const{cout<<str<<endl;}
String operator + (String s)
{
String temp;
if (strlen(str)+strlen(s.str)<80)
{
strcpy(temp.str,str);
strcat(temp.str,s.str);
}
else
{
cout<<"string is overflow";
}
return temp;
}
ostream& operator << (ostream& o,String ss)
{
o<<ss.str;
return o;
}
};
int main()
{
String s1="Merry Christmas!";
String s2="Happy Newyear!";
String s3;
s1.display();
s2.display();
s3.display();
s3=s1+s2;
s3.display();
cout<<s3;
return 0;
}
其中ostream& operator << (ostream& o,String ss)
{
o<<ss.str;
return o;
}
编译错误是什么问题,谢谢大家!!