Overloaded + operator not working correctly.

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Feb 2008
Posts: 12
Reputation: Jaycii is an unknown quantity at this point 
Solved Threads: 0
Jaycii Jaycii is offline Offline
Newbie Poster

Re: Overloaded + operator not working correctly.

 
0
  #11
Feb 17th, 2008
Not sure if I am reading that correctly or not, but the semicolon throws a syntax error in.

And if I understand you correctly, I do have an overload operator=

[I may be reading that completely wrong.]
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 419
Reputation: gerard4143 is on a distinguished road 
Solved Threads: 48
gerard4143's Avatar
gerard4143 gerard4143 is offline Offline
Posting Pro in Training

Re: Overloaded + operator not working correctly.

 
0
  #12
Feb 17th, 2008
Originally Posted by Jaycii View Post
error C2662: 'getchecking' : cannot convert 'this' pointer from 'const class Account' to 'class Account &'
Conversion loses qualifiers

  1. temp2 = (savings + right.getchecking());
  2. }
This error message tells me that the function getchecking is trying to change the value(s) of Account right. You can change the const Account & right to Account & right but you shouldn't have to if the operator+ is set up correctly
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 12
Reputation: Jaycii is an unknown quantity at this point 
Solved Threads: 0
Jaycii Jaycii is offline Offline
Newbie Poster

Re: Overloaded + operator not working correctly.

 
0
  #13
Feb 17th, 2008
Ah, I see what you mean. Well, checking goes through two classes, Customer, as well as Accounting, but getchecking shouldn't be trying to change anything.

I am checking back through, but I don't really see anything that would be causing that.

Nor can I figure out why I am getting those weird numbers.

The code is a bit to long to post it all here, which was why I was only showing parts of it.

I am completely stuck.


I recompiled the code so I could get one of the rrors. If I use the code I posted above [to add the values]
  1. CustMain = Customers[0] + Customers[1];

I get a few errors like this

class std::basic_string<_E,_Tr,_A> __cdecl std::operator +(const class std::basic_string<_E,_Tr,_A> &,const _E)' : could not deduce template argument for 'const class std::
basic_string<_E,_Tr,_A> &' from 'class Customer'
Last edited by Jaycii; Feb 17th, 2008 at 9:26 pm.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 419
Reputation: gerard4143 is on a distinguished road 
Solved Threads: 48
gerard4143's Avatar
gerard4143 gerard4143 is offline Offline
Posting Pro in Training

Re: Overloaded + operator not working correctly.

 
0
  #14
Feb 17th, 2008
I have to go so I leave with an properly working example of the operator+ and operator=
Hope this helps...Gerard4143

  1. #include <iostream>
  2.  
  3. class number
  4. {
  5. public:
  6. number(int n):num(n) {}
  7. ~number() {}
  8. number operator+(const number &rhs);
  9. number& operator=(const number & rhs);
  10. int getitsvalue() const {return num;}
  11. void setitsvalue(int val) {num = val;}
  12. friend std::ostream& operator<<(std::ostream & out, const number & rhs);
  13. private:
  14. int num;
  15. };
  16. std::ostream& operator<<(std::ostream & out, const number & rhs)
  17. {
  18. out<<rhs.getitsvalue();
  19. return out;
  20. }
  21. number& number::operator=(const number & rhs)
  22. {
  23. if (this == &rhs)
  24. return *this;
  25. setitsvalue(rhs.getitsvalue());
  26. return *this;
  27. }
  28. number number::operator+(const number &rhs)
  29. {
  30. number temp(getitsvalue() + rhs.getitsvalue());
  31. return temp;
  32. }
  33. int main (int argc, char**argv)
  34. {
  35. number x(123);
  36. number y(234);
  37. number ans(0);
  38.  
  39. ans = x + y;
  40.  
  41. std::cout<<"ans->"<<ans<<"\n";
  42. return 0;
  43. }
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 12
Reputation: Jaycii is an unknown quantity at this point 
Solved Threads: 0
Jaycii Jaycii is offline Offline
Newbie Poster

Re: Overloaded + operator not working correctly.

 
0
  #15
Feb 17th, 2008
Thank you for your help. I will look at that while I work on mine.

I also managed to get the other lines of code working, but the values still give the -9.4644e353.

I will post back if I find something else.

--Edit--

Okay, using what you posted above, I get this error:

'class Account __thiscall Account::operator +(const class Account &)' : member function already defined or declared

It is saying its already been defined or declared, but it hasn't.
Last edited by Jaycii; Feb 17th, 2008 at 9:51 pm.
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 231
Reputation: JRM will become famous soon enough JRM will become famous soon enough 
Solved Threads: 14
JRM's Avatar
JRM JRM is offline Offline
Posting Whiz in Training

Re: Overloaded + operator not working correctly.

 
0
  #16
Feb 17th, 2008
As an earlier inquiry, I was wondering where the savings variable is defined on this one.
Is it part of the Account class?


  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. }
"I like beating by head against the wall because it feels so good when I stop"
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 12
Reputation: Jaycii is an unknown quantity at this point 
Solved Threads: 0
Jaycii Jaycii is offline Offline
Newbie Poster

Re: Overloaded + operator not working correctly.

 
0
  #17
Feb 17th, 2008
savings [as well as checking] are from the Account class, but they are also members of the Customer class.
Last edited by Jaycii; Feb 17th, 2008 at 10:40 pm.
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 12
Reputation: Jaycii is an unknown quantity at this point 
Solved Threads: 0
Jaycii Jaycii is offline Offline
Newbie Poster

Re: Overloaded + operator not working correctly.

 
0
  #18
Feb 18th, 2008
I can't seem to edit my post anymore. Well, after going back through my code, I fixed part of the problem. [I can now add the values outside of the function I am calling]. But now all my values inside of that function are using numbers like the -9.5948e495.

Thanks for all the help. Going to try to see if I can fix this problem now.




--edit---
It was my = operator the entire time. Once I fixed it, everything was fine.
Last edited by Jaycii; Feb 18th, 2008 at 12:28 pm.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Other Threads in the C++ Forum
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC