whats wrong with this + op overloading code?



#include <iostream.h>
#include <conio.h>
#include <string.h>


class complex
{
private:
char a[20] , b[20];

public:
complex (char x[], char y[])
{
strcpy(a,x);
strcpy(b,y);
}
void operator+(complex c)
{
strcat(a,c.a);
strcat(b,c.b);
cout<<a<<b;
}


};
void main()
{
complex s1("Amit","NIKHIL");
complex s2("Shah","SHARMA");
getch();

}




::: m using Turbo c++ on windows 7 with dosbox :::
i am getting no error and no output with this code. 

Please tell me whats wrong with this code.
Last edited on
dont define it as a member function.

1
2
3
4
5
complex operator+(const complex&c1, const complex&c2)
{
    // use the constructor and operator+(int, int)
    return complex(/* whatever */);
}


and in your class:
 
 friend complex operator+(const complex &c1, const complex &c2);




and don't use void main() use int main()
Last edited on
i am getting no error and no output with this code.
You don't get an output because you're not calling the operator +:

1
2
3
4
complex s1("Amit","NIKHIL");
complex s2("Shah","SHARMA");

s1 + s2;
thank you code777 :)

and thanx for your suggestion mutexe
Topic archived. No new replies allowed.