Help with C++ homework

I have a multi file program that uses classes and inheritance. I have one that is stumping me. The error is: 1>c:\users\kevin kennedy\documents\visual studio 2010\projects\lab 3\lab 3\savingsaccount.cpp(11): error C2512: 'BankAccount' : no appropriate default constructor available
I am using Visual Basic 2010 and I am lost, because this error shows up in savingsAccout.h and checkingAccount.h files.

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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
class BankAccount //Names the class
{ 
protected: //variables found in main program
double newAccountNumber; 
double balance;

public: //Getter and setter functions.
BankAccount(int m, double amt, bool display); 
~BankAccount(); //Destructor
void setAccountNumber(int m);
int getAccountNumber(void);
double getbalance (void);
void depositMoney (double amt);
bool withdrawMoney (bool display);
};
#endif  

//BankAccount.cpp
#include "stdafx.h"
#include "BankAccount.h"
#include <iostream>
using namespace std;

//constructor
BankAccount::BankAccount(int num, double amt, bool display)
	{
		newAccountNumber = num;
		balance = amt;
	}

//functions
void BankAccount::setAccountNumber(int m)//a
	{
		newAccountNumber = m;
	}

int BankAccount::getAccountNumber(void) //b
	{
		int num;
		cout <<"Enter Account Number"<<endl;
		cin >> num;
		return num;
	}

double BankAccount::getbalance(void)//c
	{
		return balance;
	}

void BankAccount::depositMoney (double amt) //d
	{
		return amt;
	}

bool BankAccount::withdrawMoney(bool display) //e
{
		return display;
}
BankAccount::~BankAccount() //Implementation of destructor from class.
{}

//CheckingAccount.h
#pragma once 
#include <string>
using namespace std;

class CheckingAccount: public BankAccount //inherits base class
{ 
private: //variables found in main program
 
int transactionCount;



public: //Getter and setter functions.
CheckingAccount();
CheckingAccount(int m, bool display); 
~CheckingAccount(); //Destructor

void withdrawMoney(int m, bool display);
};

//CheckingAccount.cpp
#include "stdafx.h"
#include "BankAccount.h"
#include "CheckingAccount.h"
#include "SavingsAccount.h"
#include <iostream>
using namespace std;

//constructor
CheckingAccount::CheckingAccount()
{
	
	transactionCount = 0;
	
}
CheckingAccount::CheckingAccount(int m, bool display)
{
	
	transactionCount = m;
	
}
//functions
void CheckingAccount::withdrawMoney(int m, bool display)
{
	transactionCount=m;
}
CheckingAccount::~CheckingAccount() //Implementation of destructor from class.
{}

//SavingsAccount.h
#ifndef savingsaccount_H
#define savingsaccount_H
#include "BankAccount.h"
#include "stdafx.h"
#include <iomanip>
#include "BankAccount.h"
using namespace std;

class SavingsAccount: public BankAccount
{ 
private: //variables found in main program
double newInterestRate;
int numberOfDays;
double earnedInterest; 


public: //Getter and setter functions.
SavingsAccount();
SavingsAccount(double i, int b, double c); 
~SavingsAccount(); //Destructor
void setInterestRate(double i);
double getbalance();
double getInterestEarned();
int getNumberOfDays();
};
#endif  

//SavingsAccount.cpp
#include "stdafx.h"
#include "BankAccount.h"
#include "CheckingAccount.h"
#include "SavingsAccount.h"
#include <iostream>
using namespace std;

//constructor
SavingsAccount::SavingsAccount()
{
	newInterestRate = 0;
	numberOfDays = 0;
	earnedInterest = 0;
}
SavingsAccount::SavingsAccount(double i, int b, double c)
{
	newInterestRate = i;
	numberOfDays = b;
	earnedInterest = c;
}
//functions
 void SavingsAccount::setInterestRate(double i)
 {
 	newInterestRate = i;
 }

double SavingsAccount::getbalance()
 { 
 	double dRate = newInterestRate/365;
 	numberOfDays = rand()%8;
 	earnedInterest = balance * (earnedInterest * dRate);
 	balance += earnedInterest;
 	return balance;
 } 
 
 double SavingsAccount::getInterestEarned()
 {
 	return earnedInterest;
 }

int SavingsAccount::getNumberOfDays()
 {
 	return numberOfDays;
 }
 
SavingsAccount::~SavingsAccount()//Implementation of destructor from class.
{}

// Main.cpp
#include "stdafx.h"
#include "BankAccount.h"
#include "CheckingAccount.h"
#include "SavingsAccount.h"
#include <iostream>
#include <iomanip>
#include <windows.h>

using namespace std;

void main(void)
{
int i = 0;
int newAccountNumber = 0;
double depositAmount = 0.0;
double withdrawAmount = 0.0;
double newInterestRate = 0.0;

srand(GetTickCount());					//Seed the random number generator

//BankAccount account1;

CheckingAccount account1;
SavingsAccount account2;

cout << "Enter a six digit integer for your new savings account number:  ";
cin >> newAccountNumber;
account2.setAccountNumber(newAccountNumber);
cout << endl;

cout << "Retrieving new savings account number" << endl;
cout << "New Savings Account Number:  " << account2.getAccountNumber() << endl << endl;

cout << "Set savings account interest rate" << endl;
cin >> newInterestRate;
account2.setInterestRate(newInterestRate);

cout << "Savings account balance for account # " << account2.getAccountNumber() <<
	" is $" << account2.getbalance() << endl << endl;
cout << "Total interest earned:  " << account2.getInterestEarned() << " over " << account2.getNumberOfDays()
	<< " days" << endl << endl;

cout << showpoint << fixed << setprecision(2);

account2.depositMoney(100.0);
cout << "Savings account balance for account # " << account1.getAccountNumber() <<
	" is $" << account2.getbalance() << endl;
cout << "Total interest earned:  " << account2.getInterestEarned() << " over " << account2.getNumberOfDays()
	<< " days" << endl << endl;

for(i = 0; i < 10; i++)
	{
	cout << "Withdraw money from savings account" << endl;
	if(account2.withdrawMoney(20.0) == false)
		cout << "Withdrawal denied, insufficient funds" << endl;	

	cout << "Savings account balance for account # " << account2.getAccountNumber() <<
		" is $" << account2.getbalance() << endl;

	cout << "Total interest earned:  " << account2.getInterestEarned() << " over " << account2.getNumberOfDays()
		<< " days" << endl << endl;
	

}
}


I know the formatting may need some work but I need the fix to error. I have searched the pages here and tried a lot of different things, but I can not get rid of this error.

Any and all help is appreciated.
1
2
3
4
5
6
7
8
9
10
//class CheckingAccount: public BankAccount
CheckingAccount::CheckingAccount():
   BankAccount( 42, 0, false )
{
}

CheckingAccount::CheckingAccount(int m, bool display):
   BankAccount( 13, -3.14, true )
{
}
check out initialization lists
I am sorry, but I don't understand what you mean. I have tried so many things and your postings doesn't make sense to me.
You need a default constructor. Just add

1
2
3
4
BankAccount::BankAccount()
	{
		//Just a default action.
	}


See:
http://www.cplusplus.com/doc/tutorial/classes/ (Seach default constructor within page)

The default constructor is the constructor that takes no parameters, and it is special because it is called when an object is declared but is not initialized with any arguments.


Edit: On and also add BankAccount(); to bankaccount.h

and...

BankAccount.cpp - depositMoney method should return type double. It's void now.
and BankAccount.h prototype of depositMoney needs to be double as well.

Your getting the error because every class you derive rom bankaccount attempts to instantiate bankaccount using a default constructor which doesn't exist.
Last edited on
> You need a default constructor.
no, you don't need it.

> attempts to instantiate bankaccount using a default constructor which doesn't exist.
yes, that's the cause of the error.
however, instead of adding a default constructor you may simply call one that already exists.

See http://www.cplusplus.com/doc/tutorial/classes/ (member initialization in constructors)
Thanks for the suggestions:

I understand what ne555 was saying about not needing the default constructor, but using it was simple and no matter what I tried I could not get another constructor to be called......maybe I am overlooking the obvious.

Thanks rcast your suggestions resolved the current errors, however I have 2 more errors now.

They are:
1>CheckingAccount.obj : error LNK2019: unresolved external symbol "public: __thiscall BankAccount::BankAccount(void)" (??0BankAccount@@QAE@XZ) referenced in function "public: __thiscall CheckingAccount::CheckingAccount(void)" (??0CheckingAccount@@QAE@XZ)
1>SavingsAccount.obj : error LNK2001: unresolved external symbol "public: __thiscall BankAccount::BankAccount(void)" (??0BankAccount@@QAE@XZ)

Are these referring to the BankAccout::BankAccount(void) in the BankAccount.ccp file? That is the only place I can find them.

Let me know if you need me to post any coding I have know.

Thanks!
Last edited on
OK I think I got those 2 errors and now I have one:

1>c:\users\kevin kennedy\documents\visual studio 2010\projects\lab 3.3\lab 3.3\checkingaccount.cpp(26): error C4716: 'CheckingAccount::withdrawMoney' : must return a value

All the code I have now
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
//BankAccount.h
#ifndef bankaccount_H
#define bankaccount_H
#include <string>
using namespace std;

class BankAccount //Names the class
{ 
public: //Getter and setter functions.
BankAccount(int m, double amt, bool display);
BankAccount();
~BankAccount(); //Destructor
void setAccountNumber(int m);
int getAccountNumber(void);
double getbalance (void);
double depositMoney (double amt);
bool withdrawMoney (bool display);
double newAccountNumber; 
double balance;
};
#endif  

//BankAccount.cpp
#include "stdafx.h"
#include "BankAccount.h"
#include "SavingsAccount.h"
#include "CheckingAccount.h"
#include <iostream>
using namespace std;

//constructor
BankAccount::BankAccount(int num, double amt, bool display)
	{
		newAccountNumber = num;
		balance = amt;
	}

//functions
void BankAccount::setAccountNumber(int m)//a
	{
		newAccountNumber = m;
	}

int BankAccount::getAccountNumber(void) //b
	{
		int num;
		cout <<"Enter Account Number"<<endl;
		cin >> num;
		return num;
	}

double BankAccount::getbalance(void)//c
	{
		return balance;
	}

double BankAccount::depositMoney (double amt) //d
	{
		return amt;
	}

bool BankAccount::withdrawMoney(bool display) //e
{
		return display;
}
BankAccount::~BankAccount() //Implementation of destructor from class.
{}

//CheckingAccount.h
#pragma once 
#include <string>
using namespace std;

class CheckingAccount: public BankAccount //inherits base class
{ 
private: //variables found in main program
 
int transactionCount;



public: //Getter and setter functions.
CheckingAccount();
CheckingAccount(int m, bool display); 
~CheckingAccount(); //Destructor

bool withdrawMoney(int m, bool display);
};

//CheckingAccount.cpp
#include "stdafx.h"
#include "BankAccount.h"
#include "CheckingAccount.h"
#include "SavingsAccount.h"
#include <iostream>
using namespace std;

//constructor
CheckingAccount::CheckingAccount()
{
	
	transactionCount = 0;
	
}
CheckingAccount::CheckingAccount(int m, bool display)
{
	
	transactionCount = m;
	
}
//functions
bool CheckingAccount::withdrawMoney(int m, bool display)
{
	transactionCount=m;
}
CheckingAccount::~CheckingAccount() //Implementation of destructor from class.
{}

//SavingsAccount.h
#ifndef savingsaccount_H
#define savingsaccount_H
#include "BankAccount.h"
#include "stdafx.h"
#include <iomanip>
#include "BankAccount.h"
using namespace std;

class SavingsAccount: public BankAccount
{ 
protected: //variables found in main program
double newInterestRate;
int numberOfDays;
double earnedInterest; 


public: //Getter and setter functions.
SavingsAccount();
SavingsAccount(double i, int b, double c); 
~SavingsAccount(); //Destructor
void setInterestRate(double i);
double getbalance();
double getInterestEarned();
int getNumberOfDays();
};
#endif  

//SavingsAccount.cpp
#include "stdafx.h"
#include "BankAccount.h"
#include "CheckingAccount.h"
#include "SavingsAccount.h"
#include <iostream>
using namespace std;

//constructor
SavingsAccount::SavingsAccount()
{
	newInterestRate = 0;
	numberOfDays = 0;
	earnedInterest = 0;
}
SavingsAccount::SavingsAccount(double i, int b, double c)
{
	newInterestRate = i;
	numberOfDays = b;
	earnedInterest = c;
}
//functions
 void SavingsAccount::setInterestRate(double i)
 {
 	newInterestRate = i;
 }

double SavingsAccount::getbalance()
 { 
 	double dRate = newInterestRate/365;
 	numberOfDays = rand()%8;
 	earnedInterest = balance * (earnedInterest * dRate);
 	balance += earnedInterest;
 	return balance;
 } 
 
 double SavingsAccount::getInterestEarned()
 {
 	return earnedInterest;
 }

int SavingsAccount::getNumberOfDays()
 {
 	return numberOfDays;
 }
 
SavingsAccount::~SavingsAccount()//Implementation of destructor from class.
{}


There were no changes to the main.cpp so I did not list it.

Any suggestions to correct this?
The error is an excellent description of what is going on. Line 114 doesn't return a bool but is declared to.

Also, it's simple to call non default constructor in base class via derived classes initializer list.

See:

1
2
3
4
5
6
7
8
9
10
11
class CheckingAccount: public BankAccount //inherits base class
{
public:
int num = 1234;
double amount = 12.00;
bool display =true;


CheckingAccount() : BankAccount(num, amount, display) {}
}
Last edited on
I understand your post and have tried several variations of this but they like to create more errors than they fix. I will try again and post the results.

Thanks!
Last edited on
Topic archived. No new replies allowed.