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.

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.

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)

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.

Is Account Account::operator+(const Account & rhs);
defined in Account class or is it external to the Account class?

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?

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.

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);

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=

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

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'

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;
}

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.

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;

	}

savings [as well as checking] are from the Account class, but they are also members of the Customer class.

I can't seem to edit my post anymore. Well, after going back through my code, I fixed part of the problem. . 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.

This question has already been answered. Start a new discussion instead.