= operator overloading
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.
So how can I make sum = poly1 + poly2 to work? Thanks for the helps.
Here is the code of the overloaded "="
Polynomial & Polynomial :: operator = (Polynomial & rightSide)
if(head != NULL) // Empty the left side Polynomial frist if it's not empty
for(int i = 0; i < totalTerms; i++)
rightSide.current = rightSide.head;
for(int i = 0; i < rightSide.totalTerms; i++) // Make deep copy, create a copy of the linked list
(*this).addTerm(rightSide.current -> getCOEF(), rightSide.current -> getEXP());
rightSide.current = rightSide.current -> getLink();
I just had some trouble with overloading operators, and it seems like the following worked fine (there's probably an easier way to do it that I haven't figured out)
That might be something. Other than that, I don't know if I'd be able to help you without seeing some more code.
Polynomial& Polynomial::operator=(Polynomial& rightSide)
// ... rest of function ...
Polynomial &ref_rightSide = rightSide;
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.
I suggest ignoring aprescott_27's advice. Returning *this is correct behaviour for a copy assignment operator.
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:
Polynomial& Polynomial::Polynomial(const Polynomial& rhs)
if (this != &rhs)
Thanks for the help. I fixed it using your suggestion. Thanks.
Originally Posted by laserlight