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 401,523 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 3,376 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.
Views: 705 | 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

Re: Overloaded + operator not working correctly.

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

  #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

		temp2 = (savings + right.getchecking());
	}
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  
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.

  #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]
 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 8:26 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.

  #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

#include <iostream>

class number
{
public:
	number(int n):num(n) {}
	~number() {}
	number operator+(const number &rhs);
	number& operator=(const number & rhs);
	int getitsvalue() const {return num;}
	void setitsvalue(int val) {num = val;}
	friend std::ostream& operator<<(std::ostream & out, const number & rhs);
private:
	int num;
};
std::ostream& operator<<(std::ostream & out, const number & rhs)
{
	out<<rhs.getitsvalue();
	return out;
}
number& number::operator=(const number & rhs)
{
	if (this == &rhs)
		return *this;
	setitsvalue(rhs.getitsvalue());
	return *this;
}
number number::operator+(const number &rhs)
{
	number temp(getitsvalue() + rhs.getitsvalue());
	return temp;
}
int main (int argc, char**argv)
{
	number x(123);
	number y(234);
	number ans(0);

	ans = x + y;

	std::cout<<"ans->"<<ans<<"\n";
	return 0;
}

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.

  #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 8:51 pm.
Reply With Quote  
Join Date: Oct 2006
Location: NY
Posts: 218
Reputation: JRM will become famous soon enough JRM will become famous soon enough 
Rep Power: 3
Solved Threads: 14
JRM's Avatar
JRM JRM is offline Offline
Posting Whiz in Training

Re: Overloaded + operator not working correctly.

  #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?


Account Account::operator+(const Account &right)
	{
		Account temp;
		int temp2;
		temp2 = (savings + right.getchecking());
		temp.setTotalBalance(temp2);
		return temp;

	}
"I like beating by head against the wall because it feels so good when I stop"
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.

  #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 9:40 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.

  #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 11:28 am.
Reply With Quote  
Reply

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

DaniWeb C++ Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Other Threads in the C++ Forum

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