I am having an issue with a return value with a += operator.
the following is the specific code that is related. If more code needs to be shown I will provide it:
double operator+=(double b, const Account& c)
{
return b += c.getBalance();
}
where it is implemented in the main:
for(int i = 0; i < NUMBER_OF_ACCOUNTS; i++)
{
std::cout << i+1 << "- " << (balance += *AC[i]) << std::endl;
}
std::cout << "Total Balance: " << balance << std::endl;
output I am receiving:
1- 10302.98
2- 10302.98
3- 201.00
Total Balance: 0.00
output I am trying to get:
1- 10302.98
2- 20605.96
3- 20806.96
Total Balance: 20806.96
*characters to highlight code. That makes the code more confusing. Also, please post a minimal reproducible exampleb += c.getBalance();? Value ofbwill only change inoperator+=function scope (asbis passed by value, not by reference). I think that what you really want to do isreturn b + c.getBalance();or perhaps passbas referencedouble operator+=(double& b, const Account& c);and then increment it before returningb += c.getBalance(); return b.