943,816 Members | Top Members by Rank

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

Re: Overloaded + operator not working correctly.

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.]
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 ...
error C2662: 'getchecking' : cannot convert 'this' pointer from 'const class Account' to 'class Account &'
Conversion loses qualifiers

C++ Syntax (Toggle Plain Text)
  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
Reputation Points: 499
Solved Threads: 366
Postaholic
gerard4143 is offline Offline
2,196 posts
since Jan 2008
Feb 17th, 2008
0

Re: Overloaded + operator not working correctly.

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]
C++ Syntax (Toggle Plain Text)
  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.
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 have to go so I leave with an properly working example of the operator+ and operator=
Hope this helps...Gerard4143

C++ Syntax (Toggle Plain Text)
  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. }
Reputation Points: 499
Solved Threads: 366
Postaholic
gerard4143 is offline Offline
2,196 posts
since Jan 2008
Feb 17th, 2008
0

Re: Overloaded + operator not working correctly.

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.
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.

As an earlier inquiry, I was wondering where the savings variable is defined on this one.
Is it part of the Account class?


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. }
JRM
Reputation Points: 130
Solved Threads: 75
Practically a Master Poster
JRM is offline Offline
618 posts
since Oct 2006
Feb 17th, 2008
0

Re: Overloaded + operator not working correctly.

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

Re: Overloaded + operator not working correctly.

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