= operator overloading

This is a discussion on = operator overloading within the C++ Programming forums, part of the General Programming Boards category; Hi, everyone,
I overloaded 3 opearators in my Polynomial class, they are
"*" which returns the product of two polynomials, ...

= operator overloading

Hi, everyone,

I overloaded 3 opearators in my Polynomial class, they are

"*" which returns the product of two polynomials, return type is Polynomial.
"+" which return the sum of two polynomials, return type is Polynomial.
"=" which makes the left side have a deep copy of the right side.

But if I write sum = poly1 + poly2; in the main driver, the compiler gives me error saying illegle operand, i think it's for the "=" operand.
If I get rid of +poly2, leaving sum = poly1, it works fine. I dont know why. The + sign should perform first, return a Polynomial object, and then the = operator make a deep copy of the return object to the left side sum.

The problem might be that your parameter is not a reference to const. When you use operator+ to add poly1 + poly2, the result is a temporary object. But you cannot pass a temporary object to a function that expects a non-const reference.

The operator= parameter should be const Polynomial &.

If that's not it, make sure to post text of the full error message(s).

The copy assignment operator should take a const reference, not a reference, as its parameter. When you make the change, I would expect a compile error from the "rightSide.current = rightSide.head;" statement. What you should do is create current as a local variable, and then use this local variable to traverse rightSide.

Incidentally, notice that the part where you empty the *this Polynomial is similiar to the destructor, and the part where you make a deep copy is similiar to the copy constructor. You might want to implement the copy assignment operator in terms of the copy constructor, destructor, and a member swap function. The basic idea is to copy the right hand side object to a temporary, and then swap the *this object with the temporary, and let the destructor destroy the temporary that now has the old value of the *this object. A possible implementation would be:

I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.

The copy assignment operator should take a const reference, not a reference, as its parameter. When you make the change, I would expect a compile error from the "rightSide.current = rightSide.head;" statement. What you should do is create current as a local variable, and then use this local variable to traverse rightSide.

Incidentally, notice that the part where you empty the *this Polynomial is similiar to the destructor, and the part where you make a deep copy is similiar to the copy constructor. You might want to implement the copy assignment operator in terms of the copy constructor, destructor, and a member swap function. The basic idea is to copy the right hand side object to a temporary, and then swap the *this object with the temporary, and let the destructor destroy the temporary that now has the old value of the *this object. A possible implementation would be: