This was my homework so it is not anymore I'm just curious to solve it. Here is what this program was suppose to do. Main program was given to me by instructor. He wanted us to write a code that makes this main work. He wanted us to overload operators in order to make addition, subtraction etc... But somehow I failed whatever I do. Can you guys correct me where I'm doing wrong and what, I looked google for this unfortunately I couldn't understand. Thank you for your helps !
When it comes to compound assignment operators (+=, -=, *=, /=, etc.), the convention is to allow the function to modify the object (*this) and return a reference to the same object.
Your current operator+= function does not modify the object and returns a new, temporary object.
Your operator+ function attempts to return a constant reference to a temporary object that may or may not exist anymore.
Here are the basic semantics of binary arithmetic operators:
1 2 3 4 5 6 7
//Notice how we will return a non-const reference
Monomial& Monomial::operator+=(const Monomial&);
//Note how this time we will return a copy/value
// In addition, binary functions are usually non-member functions
// If you do make it a member function, at least mark it as read-only
// since it is not supposed to modify its parameters.
Monomial operator+(const Monomial&, const Monomial&);
And their definitions based on your code would look similar to:
1 2 3 4 5 6 7 8
Monomial& Monomial::operator+=(const Monomial& rhs){
ptr->coef += mon.coef; //Modify this object
return *this; //Now return this object to allow for "chaining", e.g.
// (m1+=m2) += m2;
}
Monomial operator+(const Monomial& lhs, const Monomial& rhs){
return Monomial(lhs) += rhs; //Basically like your original definition
}
Edit: Did not see DTSCode's response.
I think so. I have that problem all the time when I write something like: cout << FOO + BAR;
When it comes to compound assignment operators (+=, -=, *=, /=, etc.), the convention is to allow the function to modify the object (*this) and return a reference to the same object.
Your current operator+= function does not modify the object and returns a new, temporary object.
Your operator+ function attempts to return a constant reference to a temporary object that may or may not exist anymore.
Thank you ! But when I write two parameters to + operator function it underlines it and says that there are too many parameters for that operator.
Edit:
When I just tried to compile just the header with emty main it gave me 4 errors:
1>Monomial.obj : error LNK2005: "public: class Monomial & __thiscall Monomial::operator+=(struct MonomialPtr const &)" (??YMonomial@@QAEAAV0@ABUMonomialPtr@@@Z) already defined in HW5_Main.obj
1>Monomial.obj : error LNK2005: "public: class Monomial & __thiscall Monomial::operator+(struct MonomialPtr const &)" (??HMonomial@@QAEAAV0@ABUMonomialPtr@@@Z) already defined in HW5_Main.obj
My problem is now I can't do multiple operators at once like m11+m21+m31, it should be 6*x but the result that program printed was 4*x how can I solve this. Also it is same when I multiply more than 2 variables. Thank you !
I can't do multiple operators at once like m11+m21+m31, it should be 6*x but the result that program printed was 4*x how can I solve this
Your new program does not modify the coefficient or the exponent when it performs addition (instead it modifies some other member variables, which shouldn't have been added in the first place, but which you print with operator<<). So the result you're seeing is the result of the last addition in the expression.
Thanks for your reply, but because it was a homework, modifying the main was forbidden.
Your new program does not modify the coefficient or the exponent when it performs addition (instead it modifies some other member variables, which shouldn't have been added in the first place, but which you print with operator<<). So the result you're seeing is the result of the last addition in the expression.
Thank you for your reply, but when I modify them it calulates other operations wrong like:
m1+m2 = 3
when trying to use m1 again this time
m1+m3 = 6 <- it gives this result
thats why I used another member variable. But as you say now I can not completa multiple aritmatic operations successfully. Any suggestions ?