Hello, everyone. I'm having a hard time trying to figure out how to declare and initialize my dynamic array of objects. Here is the class definition and constructor definition:

class SavingsAccount : public BankAccount {
public:
	SavingsAccount(char, int, string, double, int, double=2.00, double=0.0, double=0.0);
	double getinterestR() const;
	double getaverageDailyBalance() const;
	double getinterestYtd() const;
	void setinterestR(double);
	void endOfDay();
	void print() const;
private:
	double interestR;
	double averageDailyBalance;
	double interestYtd;
	int daysTotal;
	void setaverageDailyBalance(double);
	void setinterestYtd(double);
	void interestCalc();
};
//
SavingsAccount::SavingsAccount(char type, int numID, string name, double bal, int days, double interest, double adb, double iytd) : BankAccount(type, numID, name, bal, days)
{
	setinterestR(interest);
	setaverageDailyBalance(adb);
	setinterestYtd(iytd);
}

I tried to do this in main():

SavingsAccount *accounts;
accounts = new SavingsAccount [100];

I get the compilation error: "error: no matching function for call to 'SavingsAccount::SavingsAccount()'"

I thought I needed to add a constructor with no parameters but that didn't work or I didn't do it right.
Any help will be greatly appreciated. Thanks!

Recommended Answers

All 4 Replies

You declared a single pointer, to a single SavingsAccount object.

I believe what ye' wanted to do is to create a pointer to an array of SavingsAccount pointers; each element containing the address to a new SavingsAccount object:

//Pointer-to-pointer (or an array of pointers)
SavingsAccount** accounts = new SavingsAccount*[100];

//Initialize the accounts[ ] array with new SavingsAccount objects:
for(int i=0; i<100; i++)
{
     accounts[i] = new SavingsAccount;  //you could call an overloaded constructor here and supply args if ye' wish
}

>> I thought I needed to add a constructor with no parameters but that didn't work or I didn't do it right.

Both SavingsAccount and BankAccount class need to have a default constructor in order for your allocation to succeed. Perhaps you wrote a default constructor for SavingsAccount but BankAccount doesn't have one?

@Clinton Portis
I tried your code and I got the same error at your line seven.

accounts[i] = new SavingsAccount;

@mitrkmkar
I did include a BankAccount default constructor but didn't include them for the sake of brevity.

class BankAccount {
public:
	BankAccount(char='1', int=0, string=" ", double=0.0, int=0);
//.......
};
BankAccount::BankAccount(char type, int numID, string name, double bal, int days)
{
	setaccountType(type);
	setaccountNumber(numID);
	setlastName(name);
	setbalance(bal);
	setdaysInCycle(days);
}

Thanks for the replies :)

OK, it looks like BankAccount has a default constructor whereas SavingsAccount doesn't have one. You might add default values for all SavingsAccount constructor's parameters (hence making it a default constructor) and retry.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.