I am trying out operator overloading. I have following code:
In main I have defined two Mystring object.
Following two statements work fine.
But when I do
I get compiler error. I am using cygwin compiler.
Below is the error I get. Why do you think it is happening?
Code:
class Mystring{
public:
Mystring(char* ch)
{
data = new char[strlen(ch)*4];
strcpy(data,ch);
}
friend Mystring operator+(Mystring& s1, Mystring& s2);
friend ostream& operator<<(ostream& out, Mystring& s1);
char *data;
};
Mystring operator+( Mystring& s1, Mystring& s2)
{
Mystring temp(s1.data);
strcat(temp.data,s2.data);
return temp;
}
ostream& operator<<(ostream& out, Mystring& s1)
{
out<<s1.data;
return out;
}
Code:
Mystring s1("Abhilash");
Mystring s2("Nair");
Code:
Mystring s3=s1+s2; cout<<s3<<endl;
Code:
cout<<s1+s2<<endl;
Below is the error I get. Why do you think it is happening?
Comment