   overloading operator with strings (C++)
  Hi,I''m having troubles to overload the operator + to concatenate strings!! I make the program down but there is an error that I can''t resolve!! What can I do to resolve this problem??class string{ private:   char str[80]; public:   string()    {str[0]=''0'';}  string(char s[]) {strcpy(str,s);}   string operator +=(string s) const//concatenate   {    string temp;    if(strlen(str)+strlen(s.str) < 80)    {strcpy(temp.str,str);strcat(temp.str,s.str);    }    else   cout << "\nMemory Full!!";    return temp;   }   string operator +(string s) const  //concatenate   {    string temp(str);    temp+=s.str;    return temp;   }   void print() const { cout << str; } //print};Thanks!! Fernand!! 
  class string? wouldnt that be a redefinition?    ;  Here's how you should declare your operators:const string& operator += (const string& r){  // code to concatenate r to 'this' goes here  return *this;}put operator + outside of class:inline string operator + (const string& left, const string& right){  string temp(left);  temp += right;  return temp;}   [edited by - ABC123 on February 8, 2004 3:30:00 PM];quote:Original post by DerAnged  class string? wouldnt that be a redefinition?I think no, because at main function I define the strings s1 and s2. Next, I use the comands to concatenate and to attribute the strings s3, s4, s5:   s3 += s2;  s4 = s1 + s2;  s5 = s1 + s1;There is another way to do this??Thanks!! ;quote:Original post by DerAnged  class string? wouldnt that be a redefinition?''string'' is only a redefinition if your using namespace std.You can define your own class called ''string'' as long as your not doing it within the context of namespace std. ;  Please, please, please use functions that check bounds.  Using functions like strcpy() and strcat() instead of strncpy() and strncat() are one of the main reasons that buffer exploits exist. Oh, and don''t forget about the terminating null character when you''re calculating lengths and whatnot. ;#include <string>int main(){std::string s1,s2;s1 = "hello";s2 = "world!";std::string s3 = s1+" ";s3 += s2;}Why are making your own string class? ;quote:Original post by ABC123  Here''s how you should declare your operators:const string& operator += (const string& r){  // code to concatenate r to ''this'' goes here  return *this;}put operator + outside of class:inline string operator + (const string& left, const string& right){  string temp(left);  temp += right;  return temp;}   <SPAN CLASS=editedby>[edited by - ABC123 on February 8, 2004 3:30:00 PM]</SPAN>Hi,The operator + is function very well, but I''m having troubles with operator +=!! Which is the best code to concatenate r to ''this''??Thanks!! Fernand!!;  Concatenating to ''this'' all depends on how you''re implementing your internal buffer to hold the string chars.If you''re using a dynamic buffer, you may need to grow the buffer by the length of ''r'' before concatenating.Regardless of your internal buffer, you just append ''r'' to the character buffer of ''this'' and return ''this''. ;  std::string already has a += operator that does what you want.Why are you making your own string class? 