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.
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>
usingnamespace 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>
usingnamespace 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>
usingnamespace 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"
usingnamespace 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>
usingnamespace 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>
usingnamespace 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.
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.
> 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.
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.
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
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) {}
}
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.