I'm having a bit of trouble figuring out what i'm doing wrong with my overloaded operators

Header file

#ifndef SavingsAccount_H
#define SavingsAccount_H

#include <string>
using namespace std;

class SavingsAccount
{ 
friend istream &operator >>(istream& input, SavingsAccount & );
friend ostream &operator <<(ostream& output, const SavingsAccount & );

public:
	SavingsAccount();
	SavingsAccount(string, string);
	SavingsAccount(string, string, double);
	~SavingsAccount();

	SavingsAccount& operator+=(const SavingsAccount& );
	SavingsAccount& operator-=(const SavingsAccount& );


	void setFirstName(string);
	void setLastName(string);
	void setSavingsBalance(double);
	string getFirstName() const;
	string getLastName() const;
	double  getSavingsBalance() const;

	void printInfo();
	int getNumber() const;
	int getObjCount();

	static double getIntrestRate();
	void static setIntrestRate(double);

	void CalculateNewBalance();

private: 
	string FirstName;
	string LastName;
	double SavingsBalance;
	static double AnnualIntrestRate;
	static int objcount;
	const int ObjectNumber;
};

#endif

Member functions

#include <iostream>
#include <iomanip>
#include <string>
#include <cmath>
#include "SavingsAccount.h"

using namespace std;
double SavingsAccount::AnnualIntrestRate = .05;
int SavingsAccount::objcount = 0;

SavingsAccount::SavingsAccount():ObjectNumber(++objcount)
{
	FirstName = LastName = "";
	SavingsBalance = 0;
}

SavingsAccount::SavingsAccount(string First, string Last):ObjectNumber(++objcount)
{
	setFirstName(First); 
	setLastName(Last); 
	setSavingsBalance(0);
}

SavingsAccount::SavingsAccount(string First, string Last, double Balance):ObjectNumber(++objcount)
{
	setFirstName(First); 
	setLastName(Last); 
	setSavingsBalance(Balance);
}

void SavingsAccount::setFirstName(string First)
{
	FirstName = First;
}

void SavingsAccount::setLastName(string Last)
{
	LastName = Last;
}

void SavingsAccount::setSavingsBalance(double Balance) 
{
	SavingsBalance = Balance;
}

void SavingsAccount::setIntrestRate(double Intrest)
{
	AnnualIntrestRate = Intrest;
}
 
string SavingsAccount::getFirstName() const
{
	return FirstName;
}

string SavingsAccount::getLastName() const
{
	return LastName;
}

double SavingsAccount::getIntrestRate()
{
	return AnnualIntrestRate;
}

double SavingsAccount::getSavingsBalance() const
{
	return SavingsBalance;
}

int SavingsAccount::getNumber() const
{
	return ObjectNumber;
}

void SavingsAccount::CalculateNewBalance()
{
	SavingsBalance = SavingsBalance + SavingsBalance * AnnualIntrestRate / 12;
}

void SavingsAccount::printInfo()
{
	cout << "The users serial number is " << ObjectNumber << "." << endl << "The users name is " << FirstName << " " << LastName << "." << endl;
	cout << "The users starting balance is $" << SavingsBalance << "." << endl << endl;
}


SavingsAccount::~SavingsAccount()
{

	cout << "The object with a first name of  " << FirstName << " and a last name of " << LastName << " and a serial number of " << ObjectNumber << " has gone out of scope. " << endl << endl;
}

int SavingsAccount::getObjCount()
{
	return objcount;
}

istream &operator >>(istream& input,  SavingsAccount &Info )
{
	input >> Info.FirstName;
	input >> Info.LastName;
	input >> Info.SavingsBalance;
	return input;
}

ostream &operator <<(ostream& output, const SavingsAccount &Info)
{
	output << "The users first name is " << Info.FirstName << endl;
	output << "The users last name is " << Info.LastName << endl;
	output << "The users balance is " << Info.SavingsBalance << endl;
	output << "The users intrest rate is " << Info.AnnualIntrestRate << endl;
	return output;
}

const SavingsAccount &SavingsAccount::operator+=(const SavingsAccount& double increase )
{
	 SavingsBalance += increase;
	return *this;
}

const SavingsAccount &SavingsAccount::operator-=(const SavingsAccount& double decrease )
{
	SavingsBalance -=  decrease;
	return *this;
}

main

#include <iostream>
#include <iomanip>
#include <string>
#include <cmath>
#include "SavingsAccount.h"

using namespace std;

int main()
{
	cout << setprecision(2) << fixed;

	SavingsAccount SaverOne("Margaret" , "Olson", 2000.00);
	SavingsAccount SaverTwo("Debra" , "Baxter");
	SavingsAccount SaverThree("", "");

	SaverTwo.setSavingsBalance(5000.00);

	SaverThree.setFirstName("Aruro");
	SaverThree.setLastName("Ortiz");
	SaverThree.setSavingsBalance(10000.00);

	SaverOne.CalculateNewBalance();
	SaverTwo.CalculateNewBalance();
	SaverThree.CalculateNewBalance();

	cout << "The first users new monthly balance is " << SaverOne.getSavingsBalance() << endl;
	cout << "The second user new monthly balance is " << SaverTwo.getSavingsBalance() << endl;
	cout << "The Third users new monthly balance is " << SaverThree.getSavingsBalance() << endl << endl;

	cout << "The first users information is ";
	SaverOne.printInfo();

	cout << "The second users information is ";
	SaverTwo.printInfo();

	cout << "The third users information is ";
	SaverThree.printInfo();

	SavingsAccount::setIntrestRate(.10);

	SaverOne.CalculateNewBalance();
	SaverTwo.CalculateNewBalance();
	SaverThree.CalculateNewBalance();

	cout << "The first users new monthly balance is " << SaverOne.getSavingsBalance() << endl;
	cout << "The second user new monthly balance is " << SaverTwo.getSavingsBalance() << endl;
	cout << "The Third users new monthly balance is " << SaverThree.getSavingsBalance() << endl << endl;

	cout << "The first users information is ";
	SaverOne.printInfo();

	cout << "The second users information is ";
	SaverTwo.printInfo();

	cout << "The third users information is ";
	SaverThree.printInfo();

	// programming assignment 3 main starts here
	SavingsAccount SaverFour("", "");

	cout << "enter your first name, last name, account balance: " << endl;
	cin >> SaverFour;

	cout << "The information given was: ";

	cout <<SaverFour << endl;

	cout << "the users saving balance after increase is " << endl;
	cout << SaverFour.getSavingsBalance()+= 1000.00;

	cout << "The users saving balance after decrease is ";
	cout << SaverFour.getSavingsBalance()-= 2000.00;
}

Here are the errors i get. Please note that everything else works great, I just can't get += or -= going correctly

Error 1 error C2676: binary '+=' : 'std::basic_ostream<_Elem,_Traits>' does not define this operator or a conversion to a type acceptable to the predefined operator h:\c++\programming assignment three\programming assignment two\programming assignment two\main.cpp 76

Error 2 error C2676: binary '-=' : 'std::basic_ostream<_Elem,_Traits>' does not define this operator or a conversion to a type acceptable to the predefined operator h:\c++\programming assignment three\programming assignment two\programming assignment two\main.cpp 79

Error 3 error C2511: 'const SavingsAccount &SavingsAccount::operator +=(double)' : overloaded member function not found in 'SavingsAccount' h:\c++\programming assignment three\programming assignment two\programming assignment two\member functions.cpp 123

Error 4 error C2511: 'const SavingsAccount &SavingsAccount::operator -=(double)' : overloaded member function not found in 'SavingsAccount' h:\c++\programming assignment three\programming assignment two\programming assignment two\member functions.cpp 129

5 IntelliSense: the #endif for this directive is missing h:\c++\programming assignment three\programming assignment two\programming assignment two\savingsaccount.h 7

6 IntelliSense: expected a declaration h:\c++\programming assignment three\programming assignment two\programming assignment two\savingsaccount.h 51

7 IntelliSense: the #if for this directive is missing h:\c++\programming assignment three\programming assignment two\programming assignment two\savingsaccount.h 53

8 IntelliSense: no operator "+=" matches these operands h:\c++\programming assignment three\programming assignment two\programming assignment two\main.cpp 76

9 IntelliSense: no operator "-=" matches these operands h:\c++\programming assignment three\programming assignment two\programming assignment two\main.cpp 79

Your prototypes for operator overloads do not match. In your declaration, you have:

SavingsAccount& operator+=(const SavingsAccount& );

In your definition, you have:

SavingsAccount& SavingsAccount::operator+=(const SavingsAccount& double increase);

Which is not correct syntax by the way. Either the type is 'double' or it is 'const SavingsAccount&', you have to choose. Personally, I don't see how it could make sense to add two SavingsAccount together, so I guess you intended to use "double" all along, so you should do so:

SavingsAccount& operator+=(double );

//and:
SavingsAccount& SavingsAccount::operator +=(double increase);

Okay I did what you said here is what I have so far.
header

#ifndef SavingsAccount_H
#define SavingsAccount_H

#include <string>
using namespace std;

class SavingsAccount
{ 
friend istream &operator >>(istream& input, SavingsAccount & );
friend ostream &operator <<(ostream& output, const SavingsAccount & );

public:
    SavingsAccount();
    SavingsAccount(string, string);
    SavingsAccount(string, string, double);
    ~SavingsAccount();

    SavingsAccount& operator+=(double );
    SavingsAccount& operator-=(double );


    void setFirstName(string);
    void setLastName(string);
    void setSavingsBalance(double);
    string getFirstName() const;
    string getLastName() const;
    double  getSavingsBalance() const;

    void printInfo();
    int getNumber() const;
    int getObjCount();

    static double getIntrestRate();
    void static setIntrestRate(double);

    void CalculateNewBalance();

private: 
    string FirstName;
    string LastName;
    double SavingsBalance;
    static double AnnualIntrestRate;
    static int objcount;
    const int ObjectNumber;
};

#endif

member functions

#include <iostream>
#include <iomanip>
#include <string>
#include <cmath>
#include "SavingsAccount.h"

using namespace std;
double SavingsAccount::AnnualIntrestRate = .05;
int SavingsAccount::objcount = 0;

SavingsAccount::SavingsAccount():ObjectNumber(++objcount)
{
    FirstName = LastName = "";
    SavingsBalance = 0;
}

SavingsAccount::SavingsAccount(string First, string Last):ObjectNumber(++objcount)
{
    setFirstName(First); 
    setLastName(Last); 
    setSavingsBalance(0);
}

SavingsAccount::SavingsAccount(string First, string Last, double Balance):ObjectNumber(++objcount)
{
    setFirstName(First); 
    setLastName(Last); 
    setSavingsBalance(Balance);
}

void SavingsAccount::setFirstName(string First)
{
    FirstName = First;
}

void SavingsAccount::setLastName(string Last)
{
    LastName = Last;
}

void SavingsAccount::setSavingsBalance(double Balance) 
{
    SavingsBalance = Balance;
}

void SavingsAccount::setIntrestRate(double Intrest)
{
    AnnualIntrestRate = Intrest;
}

string SavingsAccount::getFirstName() const
{
    return FirstName;
}

string SavingsAccount::getLastName() const
{
    return LastName;
}

double SavingsAccount::getIntrestRate()
{
    return AnnualIntrestRate;
}

double SavingsAccount::getSavingsBalance() const
{
    return SavingsBalance;
}

int SavingsAccount::getNumber() const
{
    return ObjectNumber;
}

void SavingsAccount::CalculateNewBalance()
{
    SavingsBalance = SavingsBalance + SavingsBalance * AnnualIntrestRate / 12;
}

void SavingsAccount::printInfo()
{
    cout << "The users serial number is " << ObjectNumber << "." << endl << "The users name is " << FirstName << " " << LastName << "." << endl;
    cout << "The users starting balance is $" << SavingsBalance << "." << endl << endl;
}


SavingsAccount::~SavingsAccount()
{

    cout << "The object with a first name of  " << FirstName << " and a last name of " << LastName << " and a serial number of " << ObjectNumber << " has gone out of scope. " << endl << endl;
}

int SavingsAccount::getObjCount()
{
    return objcount;
}

istream &operator >>(istream& input,  SavingsAccount &Info )
{
    input >> Info.FirstName;
    input >> Info.LastName;
    input >> Info.SavingsBalance;
    return input;
}

ostream &operator <<(ostream& output, const SavingsAccount &Info)
{
    output << "The users first name is " << Info.FirstName << endl;
    output << "The users last name is " << Info.LastName << endl;
    output << "The users balance is " << Info.SavingsBalance << endl;
    output << "The users intrest rate is " << Info.AnnualIntrestRate << endl;
    return output;
}

SavingsAccount& SavingsAccount::operator +=(double increase);
{
     SavingsBalance += increase;
    return *this;
}

SavingsAccount& SavingsAccount::operator -=(double increase);
{
    SavingsBalance -=  decrease;
    return *this;
}

main

#include <iostream>
#include <iomanip>
#include <string>
#include <cmath>
#include "SavingsAccount.h"

using namespace std;

int main()
{
    cout << setprecision(2) << fixed;

    SavingsAccount SaverOne("Margaret" , "Olson", 2000.00);
    SavingsAccount SaverTwo("Debra" , "Baxter");
    SavingsAccount SaverThree("", "");

    SaverTwo.setSavingsBalance(5000.00);

    SaverThree.setFirstName("Aruro");
    SaverThree.setLastName("Ortiz");
    SaverThree.setSavingsBalance(10000.00);

    SaverOne.CalculateNewBalance();
    SaverTwo.CalculateNewBalance();
    SaverThree.CalculateNewBalance();

    cout << "The first users new monthly balance is " << SaverOne.getSavingsBalance() << endl;
    cout << "The second user new monthly balance is " << SaverTwo.getSavingsBalance() << endl;
    cout << "The Third users new monthly balance is " << SaverThree.getSavingsBalance() << endl << endl;

    cout << "The first users information is ";
    SaverOne.printInfo();

    cout << "The second users information is ";
    SaverTwo.printInfo();

    cout << "The third users information is ";
    SaverThree.printInfo();

    SavingsAccount::setIntrestRate(.10);

    SaverOne.CalculateNewBalance();
    SaverTwo.CalculateNewBalance();
    SaverThree.CalculateNewBalance();

    cout << "The first users new monthly balance is " << SaverOne.getSavingsBalance() << endl;
    cout << "The second user new monthly balance is " << SaverTwo.getSavingsBalance() << endl;
    cout << "The Third users new monthly balance is " << SaverThree.getSavingsBalance() << endl << endl;

    cout << "The first users information is ";
    SaverOne.printInfo();

    cout << "The second users information is ";
    SaverTwo.printInfo();

    cout << "The third users information is ";
    SaverThree.printInfo();

    // programming assignment 3 main starts here
    SavingsAccount SaverFour("", "");

    cout << "enter your first name, last name, account balance: " << endl;
    cin >> SaverFour;

    cout << "The information given was: ";

    cout <<SaverFour << endl;

    cout << "the users saving balance after increase is " << endl;
    cout << SaverFour.getSavingsBalance()+= 1000.00;

    cout << "The users saving balance after decrease is ";
    cout << SaverFour.getSavingsBalance()-= 2000.00;
}

Errors include:

Error   4   error C2447: '{' : missing function header (old-style formal list?) h:\c++\programming assignment three\programming assignment two\programming assignment two\member functions.cpp  123

Error   6   error C2447: '{' : missing function header (old-style formal list?) h:\c++\programming assignment three\programming assignment two\programming assignment two\member functions.cpp  129

Error   2   error C2676: binary '-=' : 'std::basic_ostream<_Elem,_Traits>' does not define this operator or a conversion to a type acceptable to the predefined operator h:\c++\programming assignment three\programming assignment two\programming assignment two\main.cpp  79

Error   1   error C2676: binary '+=' : 'std::basic_ostream<_Elem,_Traits>' does not define this operator or a conversion to a type acceptable to the predefined operator h:\c++\programming assignment three\programming assignment two\programming assignment two\main.cpp  76

Error   5   error C2761: 'SavingsAccount &SavingsAccount::operator -=(double)' : member function redeclaration not allowed  h:\c++\programming assignment three\programming assignment two\programming assignment two\member functions.cpp  128

Error   3   error C2761: 'SavingsAccount &SavingsAccount::operator +=(double)' : member function redeclaration not allowed  h:\c++\programming assignment three\programming assignment two\programming assignment two\member functions.cpp  122

    8   IntelliSense: expected a declaration    h:\c++\programming assignment three\programming assignment two\programming assignment two\member functions.cpp  123

    9   IntelliSense: member function "SavingsAccount::operator-=" may not be redeclared outside its class  h:\c++\programming assignment three\programming assignment two\programming assignment two\member functions.cpp  128

    7   IntelliSense: member function "SavingsAccount::operator+=" may not be redeclared outside its class  h:\c++\programming assignment three\programming assignment two\programming assignment two\member functions.cpp  122

    10  IntelliSense: expected a declaration    h:\c++\programming assignment three\programming assignment two\programming assignment two\member functions.cpp  129

Edited 3 Years Ago by mike_2000_17: Fixed formatting

You left semi-columns at the end of your definition prototypes, remove them:

SavingsAccount& SavingsAccount::operator -=(double increase)  //notice no ';' here.
{
	SavingsBalance -=  decrease;
	return *this;
}

Your other errors are due to the lines at the end of your main function that look like this:

cout << SaverFour.getSavingsBalance() += 1000.00;

The problem here is that the compiler evaluates the << operator first and then tries to evaluate the +=, resulting in trying to add-store 1000.0 in the ostream object, which is not possible. You solve this problem with a simple set of parentheses:

cout << ( SaverFour.getSavingsBalance() += 1000.00 ) ;

Well, I can't thank you enough for your help so far. My book has kind of been terrible in showing us the syntax on how you'd do this.

I still have two last errors though. I tinkered with it and couldn't figure it out.

in main

cout << "the users saving balance after increase is " << endl;
	cout << (SaverFour.getSavingsBalance()+=1000.00) ;

	cout << "The users saving balance after decrease is ";
	cout << (SaverFour.getSavingsBalance()-= 1000.00) ;

Error 1 error C2106: '+=' : left operand must be l-value h:\c++\programming assignment three\programming assignment two\programming assignment two\main.cpp 76

Error 2 error C2106: '-=' : left operand must be l-value h:\c++\programming assignment three\programming assignment two\programming assignment two\main.cpp 79

The error is due to the fact that you cannot add-and-assign to a temporary value. In this case, the value returned by the getSavingsBalance is a temporary value (formally called an "rvalue"). Because the return value is a double, you could simply do:

cout << (SaverFour.getSavingsBalance() + 1000.00) ;

Or, if you wish to use your operator += overload, you need to apply it to the SaverFour object itself, as so:

cout << (SaverFour += 1000.0);

// or, to just print the amount:
cout << (SaverFour += 1000.0).getSavingsBalance();

Works like a champ, and now I understand what I was doing wrong with syntax on overloaded operators :) Thanks so much, you're great at explaining!

Edited 5 Years Ago by Ravic85: n/a

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