Overloading +=

I am working on a program to create two inherited class (savings and checking)from one base class (account) and I have to overload += and-= to work on my defined classes (savings and checking). But I am getting this syntax error and it is driving me crazy.

Right now I am just doing the overload for the Checking class and will add it to the Savings class when done (so I did't include the savings.cpp or savings.h).

In the += overloaded funtion I am just displaying a message and returning this.
But I am guessing that I just need to have a line like this

balance = balance + Right;

ERROR
47 C:\Dev-Cpp\lab7.cpp no match for 'operator+=' in '*(&checking)->std::vector<_Tp, _Alloc>::operator[] [with _Tp = Account*, _Alloc = std::allocator<Account*>](((unsigned int)(acct - 1))) += amt'

MAIN DRIVER PROGRAM
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#include <vector>
#include <iostream>
using namespace std;

#include "savings.h"
#include "checking.h"

void printMenu();

int main(  )
{
vector <Account *> checking(50) ;
vector <Savings *> savings(50);
int accNumS = 0, accNumC = 0, acct;
double bal = 0, amt = 0;
int choice;


do
{
printMenu();
cout << "Enter choice : ";
cin >> choice;

  switch(choice)
  {		
  case 1:
			cout << "\nEnter balance for account #" << accNumC+1 << ": ";
			cin >> bal;
              	checking[accNumC] = new Checking(accNumC+1,bal);
              	accNumC++;
              	break;
  case 2:
			cout << "\nEnter balance for account #" << accNumS+1 << ": ";
			cin >> bal;
              	savings[accNumS] = new Savings(accNumS+1,bal);
              	accNumS++;
              	break;

		

 case 3:
		  	cout << "\nWhich checking account: ";
           	cin >> acct;
                 	cout << "Amount of deposit: ";
		   	cin >> amt;
                 	(*checking[acct-1]) += amt;

              	break;
 case 4:
		  	cout << "\nWhich checking account: ";
           	cin >> acct;
            cout << "Amount of withdrawal: ";
		   	cin >> amt;
//                 	(*checking[acct-1]) -= amt;
              	break;
 case 5:
		  	cout << "\nWhich savings account: ";
           	cin >> acct;
            cout << "Amount of deposit: ";
		   	cin >> amt;
//                 	(*savings[acct-1]) += amt;
              	break;
 case 6:
		  	cout << "\nWhich savings account: ";
           	cin >> acct;
            cout << "Amount of withdrawal: ";
		   	cin >> amt;
//                 	(*savings[acct-1]) -= amt;
              	break;
 case 7:
		  	cout << "\nPrint All Accounts: " <<endl;
           	break;
 case 8:
		  	cout << "EXIT " <<endl;
           	break;
              	
  }
}while(choice !=8);

return 0;
}
void printMenu()
{
cout <<"\n\n###################################################"<< endl;
cout <<"#  Enter one of the following                     #"<< endl; 
cout <<"#  1) Create a new Checking Account               #"<< endl;  
cout <<"#  2) Create a new Savings Account                #"<< endl;   
cout <<"#  3) Make a Deposit for a Checking Account       #"<< endl;   
cout <<"#  4) Make a Withdrawal for a Checking Account    #"<< endl;   
cout <<"#  5) Make a Deposit a Savings Account            #"<< endl;   
cout <<"#  6) Make a Withdrawal for a Savings Account     #"<< endl;   
cout <<"#  7) Display all Account                         #"<< endl;   
cout <<"#  8) Exit                                        #"<< endl;   
cout <<"###################################################"<< endl;

return;
 }


CHECKING.CPP
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include "checking.h"

Checking::Checking(int acct, double bal)
 :Account(acct, bal)
{
                      

}

Checking & Checking::operator+=(const Checking  &Right)
{
cout <<"+=";
return *this;

}


CHECKING.H
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#ifndef CHECKING_H
#define CHECKING_H

#include "account.h"

class Checking : public Account
{
  public:
         Checking (int, double);
//         Checking operator += (int);
         Checking & Checking::operator+=(const Checking  &Right);
  private:
          int Cbal;      


};

#endif 
Try

*(checking[acct-1]) += amt;

 
*(checking[acct-1]) += amt;


Gives me this error
 
49 C:\Dev-Cpp\lab7.cpp no match for 'operator+=' in '*(&checking)->std::vector<_Tp, _Alloc>::operator[] [with _Tp = Account*, _Alloc = std::allocator<Account*>](((unsigned int)(acct - 1))) += amt' 
you added += to your Checking class, but you're not using Checking in your code. You're using Account and Savings.
Oh, ok, now that I looked more closely I see the problem.

You are trying to call Checking::operator+=( double ) because amt is a double, but
you only provide Checking::operator+=( const Checking& ). You need to provide the
former operator as well.
Disch I am not sure what you mean. :( I just was trying to get the += overloaded for the checking and once that was working I would just copy it to use it for the savings object. I thought that since savings and checking were created thru inheritance that they inherited the public member functions of account.

This is my new Checking.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include "checking.h"

Checking::Checking(int acct, double bal)
 :Account(acct, bal)
{
                      

}
Checking& Checking::operator+=(const double  &Right)
{
balance=balance+right;
return *this;

}


New header
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#ifndef CHECKING_H
#define CHECKING_H

#include "account.h"

class Checking : public Account
{
  public:
         Checking (int, double);
         Checking& Checking::operator+=(const double  &Right);

 
  private:
          int Cbal;      


};

#endif


New Error
 
49 C:\Dev-Cpp\lab7.cpp no match for 'operator+=' in '*(&checking)->std::vector<_Tp, _Alloc>::operator[] [with _Tp = Account*, _Alloc = std::allocator<Account*>](((unsigned int)(acct - 1))) += amt' 

DRIVER
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#include <vector>
#include <iostream>
using namespace std;

#include "savings.h"
#include "checking.h"

void printMenu();

int main(  )
{
vector <Account *> checking(50) ;
vector <Savings *> savings(50);
int accNumS = 0, accNumC = 0, acct;
double bal = 0, amt = 0;
int choice;

//Checking c1(1,200.00);
//Checking c2(2, 500.00);

do
{
printMenu();
cout << "Enter choice : ";
cin >> choice;

  switch(choice)
  {		
  case 1:
			cout << "\nEnter balance for account #" << accNumC+1 << ": ";
			cin >> bal;
              	checking[accNumC] = new Checking(accNumC+1,bal);
              	accNumC++;
              	break;
  case 2:
			cout << "\nEnter balance for account #" << accNumS+1 << ": ";
			cin >> bal;
              	savings[accNumS] = new Savings(accNumS+1,bal);
              	accNumS++;
              	break;



 case 3:
		  	cout << "\nWhich checking account: ";
           	cin >> acct;
                 	cout << "Amount of deposit: ";
		   	cin >> amt;
                 	(*checking[acct-1]) += amt;


              	break;
 case 4:
		  	cout << "\nWhich checking account: ";
           	cin >> acct;
            cout << "Amount of withdrawal: ";
		   	cin >> amt;
//                 	(*checking[acct-1]) -= amt;
              	break;
 case 5:
		  	cout << "\nWhich savings account: ";
           	cin >> acct;
            cout << "Amount of deposit: ";
		   	cin >> amt;
//                 	(*savings[acct-1]) += amt;
              	break;
 case 6:
		  	cout << "\nWhich savings account: ";
           	cin >> acct;
            cout << "Amount of withdrawal: ";
		   	cin >> amt;
//                 	(*savings[acct-1]) -= amt;
              	break;
 case 7:
		  	cout << "\nPrint All Accounts: " <<endl;
           	break;
 case 8:
		  	cout << "EXIT " <<endl;
           	break;
              	
  }
}while(choice !=8);

return 0;
}
void printMenu()
{
cout <<"\n\n###################################################"<< endl;
cout <<"#  Enter one of the following                     #"<< endl; 
cout <<"#  1) Create a new Checking Account               #"<< endl;  
cout <<"#  2) Create a new Savings Account                #"<< endl;   
cout <<"#  3) Make a Deposit for a Checking Account       #"<< endl;   
cout <<"#  4) Make a Withdrawal for a Checking Account    #"<< endl;   
cout <<"#  5) Make a Deposit a Savings Account            #"<< endl;   
cout <<"#  6) Make a Withdrawal for a Savings Account     #"<< endl;   
cout <<"#  7) Display all Account                         #"<< endl;   
cout <<"#  8) Exit                                        #"<< endl;   
cout <<"###################################################"<< endl;

return;
 }
Your vector is declared as a vector of pointers to base classes. You will have to make
a (pure) virtual operator+=( double ) in the base class (Account).

Topic archived. No new replies allowed.