I am trying to use an overloaded + operator to find the total balance for two values. I am messing up with the way to set them up, or forgetting something. I am getting values such as -9.543e2433
The code I am using for the operator is
Account operator*(Account %26amp;right)
{
Account temp;
int temp2;
temp2 = (Balance * right.getBalance());
temp.setExistingBalance(temp2);
return temp;
}
I've looked for examples to see if I did it wrong, but from what I have looked at, it should have worked, unless I am not noticing something.
Any tips or suggestions on how I could go about fixing it would help me greatly.
Overloaded + operator not working correctly. [C++]?
Unless I'm misreading something, it looks like you used '*' when you said you wanted to overload the '+' operator. Unless that's just a typo in the YA! question...
addl: You may know more than me, but, when I've set up overloaded operators in C++ with the intent of just adding numbers, I always had the function returning a variable, like double or int. It looks like you're trying to return an object. Is this on purpose? If I know the reason behind it I may be able to help.
addl: Is this overloaded operator a part of the object you made? If not I'd try to make it a member. Seems to work better that way.
addl: Here's how I would write it. See if this helps.
void Account::operator+(Account %26amp;right)
{
int temp;
temp = savings + right.getChecking();
right.setTotalBalance(temp);
}
Basically, it sounds like you don't need it to return a value. You just need it to set the values. Because I don't know your entire code, you may need to tweak this to fit your program especially since I don't know which object needs the total balance set. If you sometimes need it to return a value, just change void to double or int or whatever you need, and have it return variable 'temp'. Like I said, this may not work exactly because I don't know all the code. But, based on my experience AND a couple of C++ books I have here, this should point you in the right direction.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment