M
Master of C++
Hello Group,
Please take a look at the following (simple) C++ code (at the end of
this post) that does operator overloading. It gives me the following
compile error:
complex.cpp: In function `int main()':
complex.cpp:59: no match for `cmplx& = cmplx' operator
complex.cpp:26: candidates are: cmplx& cmplx:perator=(cmplx&)
I am guessing that the error has something to do with the way I have
defined operator=. But I am not sure what went wrong. I am passing all
arguments as references because in my actual code the objects that I am
dealing with a large and it would be inefficient to pass them as copies
to operator=(). I am guessing the C++ interprets the line,
c = a + b;
as
c.operator=(operator+(a, b));
which should work based on the definitions in the code listed below:
class cmplx
{
double real, imag;
public :
cmplx()
{
;
}
cmplx(double realP, double imagP)
{
real = realP;
imag = imagP;
}
cmplx(cmplx &another)
{
real = another.real;
imag = another.imag;
}
cmplx &operator=(cmplx &another)
{
real = another.real;
imag = another.imag;
return *this;
}
cmplx& operator+=(cmplx &another)
{
real += another.real;
imag += another.imag;
return *this;
}
void print(void)
{
printf("[%d + j %d]", real, imag);
}
};
cmplx operator+(cmplx &first, cmplx &second)
{
cmplx tmp(first);
tmp += second;
return tmp;
}
int main(void)
{
cmplx *a, b, c;
a = new cmplx(1, 1);
b = *a;
c = b + *a;
printf("a = "); a->print(); printf("\n");
printf("b = "); b.print(); printf("\n");
printf("c = "); c.print(); printf("\n");
delete a;
}
Thanks a lot !
-Vijay.
Please take a look at the following (simple) C++ code (at the end of
this post) that does operator overloading. It gives me the following
compile error:
complex.cpp: In function `int main()':
complex.cpp:59: no match for `cmplx& = cmplx' operator
complex.cpp:26: candidates are: cmplx& cmplx:perator=(cmplx&)
I am guessing that the error has something to do with the way I have
defined operator=. But I am not sure what went wrong. I am passing all
arguments as references because in my actual code the objects that I am
dealing with a large and it would be inefficient to pass them as copies
to operator=(). I am guessing the C++ interprets the line,
c = a + b;
as
c.operator=(operator+(a, b));
which should work based on the definitions in the code listed below:
class cmplx
{
double real, imag;
public :
cmplx()
{
;
}
cmplx(double realP, double imagP)
{
real = realP;
imag = imagP;
}
cmplx(cmplx &another)
{
real = another.real;
imag = another.imag;
}
cmplx &operator=(cmplx &another)
{
real = another.real;
imag = another.imag;
return *this;
}
cmplx& operator+=(cmplx &another)
{
real += another.real;
imag += another.imag;
return *this;
}
void print(void)
{
printf("[%d + j %d]", real, imag);
}
};
cmplx operator+(cmplx &first, cmplx &second)
{
cmplx tmp(first);
tmp += second;
return tmp;
}
int main(void)
{
cmplx *a, b, c;
a = new cmplx(1, 1);
b = *a;
c = b + *a;
printf("a = "); a->print(); printf("\n");
printf("b = "); b.print(); printf("\n");
printf("c = "); c.print(); printf("\n");
delete a;
}
Thanks a lot !
-Vijay.