User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 375,195 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,156 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser:
Views: 650 | Replies: 17 | Solved
Reply
Join Date: Feb 2008
Posts: 12
Reputation: Jaycii is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
Jaycii Jaycii is offline Offline
Newbie Poster

Overloaded + operator not working correctly.

  #1  
Feb 17th, 2008
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 &right)
{
Account temp;
int temp2;
temp2 = (savings + right.getchecking());
temp.setTotalBalance(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.
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 10,199
Reputation: Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of 
Rep Power: 34
Solved Threads: 824
Moderator
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Most Valuable Poster

Re: Overloaded + operator not working correctly.

  #2  
Feb 17th, 2008
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.
'Politics' is made up of two words, 'poli,' which is Greek for 'many,' and 'tics,' which are blood-sucking insects.
- Gore Vidal
Being ignorant is not so much a shame as being unwilling to learn. - Benjamin Franklin
Reply With Quote  
Join Date: Feb 2008
Posts: 12
Reputation: Jaycii is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
Jaycii Jaycii is offline Offline
Newbie Poster

Re: Overloaded + operator not working correctly.

  #3  
Feb 17th, 2008
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.
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 7:04 pm.
Reply With Quote  
Join Date: Jan 2008
Posts: 72
Reputation: gerard4143 is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 9
gerard4143 gerard4143 is offline Offline
Junior Poster in Training

Re: Overloaded + operator not working correctly.

  #4  
Feb 17th, 2008
Account operator+(Account &right)

should be

Account Account::operator+(const Account &right)
Last edited by gerard4143 : Feb 17th, 2008 at 7:07 pm.
Reply With Quote  
Join Date: Feb 2008
Posts: 12
Reputation: Jaycii is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
Jaycii Jaycii is offline Offline
Newbie Poster

Re: Overloaded + operator not working correctly.

  #5  
Feb 17th, 2008
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
	Account Account::operator+(const Account &right)
	{
		Account temp;
		int temp2;
		temp2 = (savings + right.getchecking());
		temp.setTotalBalance(temp2);
		return temp;

	}

--

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 7:14 pm.
Reply With Quote  
Join Date: Jan 2008
Posts: 72
Reputation: gerard4143 is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 9
gerard4143 gerard4143 is offline Offline
Junior Poster in Training

Re: Overloaded + operator not working correctly.

  #6  
Feb 17th, 2008
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 7:20 pm.
Reply With Quote  
Join Date: Feb 2008
Posts: 12
Reputation: Jaycii is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
Jaycii Jaycii is offline Offline
Newbie Poster

Re: Overloaded + operator not working correctly.

  #7  
Feb 17th, 2008
It is defined inside of the Account class.
Reply With Quote  
Join Date: Jan 2008
Posts: 72
Reputation: gerard4143 is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 9
gerard4143 gerard4143 is offline Offline
Junior Poster in Training

Re: Overloaded + operator not working correctly.

  #8  
Feb 17th, 2008
Originally Posted by Jaycii View Post
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
	Account Account::operator+(const Account &right)
	{
		Account temp;
		int temp2;
		temp2 = (savings + right.getchecking());
		temp.setTotalBalance(temp2);
		return temp;

	}


--

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?
Reply With Quote  
Join Date: Feb 2008
Posts: 12
Reputation: Jaycii is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
Jaycii Jaycii is offline Offline
Newbie Poster

Re: Overloaded + operator not working correctly.

  #9  
Feb 17th, 2008
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

CustMain = Customers[0] + Customers[1];

[CustMain would be from Customer CustMain]

and I was going to use that with the
 cout << total.getTotalBalance << endl; 
since it is what was changed in the operator.

That doesn't work though.

--Edit--

I also tried using
 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 7:43 pm.
Reply With Quote  
Join Date: Jan 2008
Posts: 72
Reputation: gerard4143 is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 9
gerard4143 gerard4143 is offline Offline
Junior Poster in Training

Re: Overloaded + operator not working correctly.

  #10  
Feb 17th, 2008
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 8:00 pm.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb C++ Marketplace
Thread Tools Display Modes

Other Threads in the C++ Forum

All times are GMT -4. The time now is 2:15 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC