943,844 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 1838
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Feb 17th, 2008
0

Overloaded + operator not working correctly.

Expand Post »
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

C++ Syntax (Toggle Plain Text)
  1. Account operator+(Account &right)
  2. {
  3. Account temp;
  4. int temp2;
  5. temp2 = (savings + right.getchecking());
  6. temp.setTotalBalance(temp2);
  7. return temp;
  8. }

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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Jaycii is offline Offline
12 posts
since Feb 2008
Feb 17th, 2008
0

Re: Overloaded + operator not working correctly.

It looks like it should work. Use your compiler's debugger and set a break point on that return statement then when the program gets to that point you can inspect the value of all the variables. If you don't know how to do that then just put some cout statements to display the variable values.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,951 posts
since Aug 2005
Feb 17th, 2008
0

Re: Overloaded + operator not working correctly.

I have cout statements to show the values for them and they are showing up correctly.

I am thinking it has to do with the way I am using it, I followed an example and I may have left something out.
C++ Syntax (Toggle Plain Text)
  1. cout << total.getTotalBalance() << endl;

That is how I am trying to use it to find the values.

-- Edit--

I am working on a way to try and fix it, not sure if it will work. If it does, I will post it to show how I fixed it. (not quite sure it will work)
Last edited by Jaycii; Feb 17th, 2008 at 8:04 pm.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Jaycii is offline Offline
12 posts
since Feb 2008
Feb 17th, 2008
0

Re: Overloaded + operator not working correctly.

C++ Syntax (Toggle Plain Text)
  1. Account operator+(Account &right)
  2.  
  3. should be
  4.  
  5. Account Account::operator+(const Account &right)
Last edited by gerard4143; Feb 17th, 2008 at 8:07 pm.
Reputation Points: 499
Solved Threads: 367
Postaholic
gerard4143 is offline Offline
2,197 posts
since Jan 2008
Feb 17th, 2008
0

Re: Overloaded + operator not working correctly.

If I switch it over to the way you just posted, I get the following error:

error C2662: 'getchecking' : cannot convert 'this' pointer from 'const class Account' to 'class Account &'
Conversion loses qualifiers

That is when I use this code
C++ Syntax (Toggle Plain Text)
  1. Account Account::operator+(const Account &right)
  2. {
  3. Account temp;
  4. int temp2;
  5. temp2 = (savings + right.getchecking());
  6. temp.setTotalBalance(temp2);
  7. return temp;
  8.  
  9. }

--

And the way I was trying to fix it didn't work, so back to step one, looking for what could be doing it.

-- Edit--

Looking back at that, since you input the data after running the program, none of the values are constant, which is the reason for that error I believe.
Last edited by Jaycii; Feb 17th, 2008 at 8:14 pm.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Jaycii is offline Offline
12 posts
since Feb 2008
Feb 17th, 2008
0

Re: Overloaded + operator not working correctly.

Is Account Account::operator+(const Account & rhs);
defined in Account class or is it external to the Account class?
Last edited by gerard4143; Feb 17th, 2008 at 8:20 pm.
Reputation Points: 499
Solved Threads: 367
Postaholic
gerard4143 is offline Offline
2,197 posts
since Jan 2008
Feb 17th, 2008
0

Re: Overloaded + operator not working correctly.

It is defined inside of the Account class.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Jaycii is offline Offline
12 posts
since Feb 2008
Feb 17th, 2008
0

Re: Overloaded + operator not working correctly.

Click to Expand / Collapse  Quote originally posted by Jaycii ...
If I switch it over to the way you just posted, I get the following error:

error C2662: 'getchecking' : cannot convert 'this' pointer from 'const class Account' to 'class Account &'
Conversion loses qualifiers

That is when I use this code
C++ Syntax (Toggle Plain Text)
  1. Account Account::operator+(const Account &right)
  2. {
  3. Account temp;
  4. int temp2;
  5. temp2 = (savings + right.getchecking());
  6. temp.setTotalBalance(temp2);
  7. return temp;
  8.  
  9. }


--

And the way I was trying to fix it didn't work, so back to step one, looking for what could be doing it.

-- Edit--

Looking back at that, since you input the data after running the program, none of the values are constant, which is the reason for that error I believe.
does getchecking() change Account in any way?
Reputation Points: 499
Solved Threads: 367
Postaholic
gerard4143 is offline Offline
2,197 posts
since Jan 2008
Feb 17th, 2008
0

Re: Overloaded + operator not working correctly.

getchecking() doesn't, but TotalBalance which is a member of Account does get changed when the two values are added.


I think I know the problem, but not sure how to fix it.

The variables for savings and checking are showing up, so those are fine. However, I don't think they are getting used inside of the + operator, so its throwing out a strange number.

I've tried a few different ways of combing them, such as

C++ Syntax (Toggle Plain Text)
  1. CustMain = Customers[0] + Customers[1];

[CustMain would be from Customer CustMain]

and I was going to use that with the
C++ Syntax (Toggle Plain Text)
  1. cout << total.getTotalBalance << endl;
since it is what was changed in the operator.

That doesn't work though.

--Edit--

I also tried using
C++ Syntax (Toggle Plain Text)
  1. cout << CustMain.getTotalBalance << endl;
, from an example I looked at, but that just brings up quite a few errors.
Last edited by Jaycii; Feb 17th, 2008 at 8:43 pm.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Jaycii is offline Offline
12 posts
since Feb 2008
Feb 17th, 2008
0

Re: Overloaded + operator not working correctly.

I think I can clear something up, are you overloading operator=(equals operator) ?
If you are not then set your operator+ to:

Account Account::operator+(Account & right);
Last edited by gerard4143; Feb 17th, 2008 at 9:00 pm.
Reputation Points: 499
Solved Threads: 367
Postaholic
gerard4143 is offline Offline
2,197 posts
since Jan 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: How to delete files from a direct pathway?
Next Thread in C++ Forum Timeline: simple question regarding array





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC