plz chk i want to overload compound assignment operator bt its gving me some error

#include <iostream>
using namespace std;

class strng
{
  char s[30];
    public:
           strng()
           {
                   strcpy(s,"");
                   }
                    void getstring()
                   {
                        cout<<"enter the sting:";
                        cin>>s;
                        }
                        void displaystring()
                        {
                             cout <<"the string is:"<<s<<endl;
                             }

                   
                   string operator +=(string &t);
                   };
                   
                        string strng:: operator +=(string &t)
                             {
                                    strng str;
                                    strcpy (str.s,"");
                                    strcat (str.s,s);
                                    strcat (str.s,s.t);
                                    return s;}
                                     main
                                    {
                                         string string1;
                                         string string2;
                                         string1.getstring();
                                         string2.getstring();
                                         string string1=string1+string2;
                                         string1.displaystring();
                             

}

name your class something else because string is already a class in <string> header file.

line 10: That sure is a waste of time to call strcpy() with an empty string. All you have to do is this: s[0] = '\0'; line 25: delete the string:: part because its not needed when defined inside the class.


line 33: what is main doing there? Its inside the c++ class declaration. I suppose you probably meant that to be int main() function. If that is currect then you don't have a closing } for the class.

You might also want to set your compiler's IDE to use spaces instead of tabs so that they look the same on all IDE's and web browsers.

now chk wats wong at line 26 and 34

#include <iostream>
using namespace std;

class str
{
  char s[30];
    public:
           str()
           {
                   strcpy(s,"");
                   }
                    void getstring()
                   {
                        cout<<"enter the sting:";
                        cin>>s;
                        }
                        void displaystring()
                        {
                             cout <<"the string is:"<<s<<endl;
                             }

                   
                          
                         string operator +=(string &t)
                             {
                                    strcat(s,t);
                                    
                                    return s;
                                    }
                                    };
                                    
                                    
                                    int main
                                    {
                                         string string1;
                                         string string2;
                                         string1.getstring();
                                         string2.getstring();
                                         string string1+= string string2;
                                         string1.displaystring();
                             

}

>now chk wats wong at line 26 and 34
The same thing that was wrong before: you're trying to write code too far beyond your ability. Start smaller and work your way up. At least then you won't have to rely on other people to hold your hand the whole time.

but my main function is not included in class

line 26: strcat() requires the second argument to be const char*, not your string class. But that's easy to fix, just strcat(s, t.s) .

line 34: the problem is on line 33, not line 34. Look carefully at line 33 and you should be able to see the error.

>>but my main function is not included in class
It was in your original post. I see you added the }; that was missing from that class.

now chk it plz

#include <iostream>
using namespace std;

class str
{
  char s[30];
    public:
           str()
           {
                   strcpy(s,"");
                   }
                    void getstring()
                   {
                        cout<<"enter the sting:";
                        cin>>s;
                        }
                        void displaystring()
                        {
                             cout <<"the string is:"<<s<<endl;
                             }

                   
                          
                         string operator+= (string &t)
                             {
                                    
                                    strcat(s,t.s);
                                    
                                    return s;
                                    }
                                    };
                                    
                                    
                                     main()
                                    {
                                         string string1;
                                         string string2;
                                         string1.getstring();
                                         string2.getstring();
                                         string string1+= string string2;
                                         string1.displaystring();
                             

}
commented: Fail. -4
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.