-1

I can not figure out how to fix this constructor. It keeps returning the error:"no matching function for call to 'JMTDSavingsAccount::JMTDSavingsAccount()' " This is weird because the error is on the constructor for the child class (child class: JMTDCheckingAccount, parent: JMTDSavingsAccount). Isn't the constructor not passed on to the child class?

Header for child class:

#define JMTDCHECKINGACCOUNT_H
#include <iostream>
#include <iomanip>
#include <string>
#include <sstream>
#include "JMTDSavingsAccount.h"


class JMTDCheckingAccount : public JMTDSavingsAccount {
private:
    double transaction_fee();

public:
    JMTDCheckingAccount(double balance, std::string account_number, double transaction_fee);
    void set_transaction_fee(double fee);
    double get_transaction_fee();

    virtual void deposit(double amount);
    virtual void withdraw(double amount);
    std::string a_to_string();
};


#endif

source file for child class: (only the part we are dealing with)

#include "JMTDCheckingAccount.h"
#include "JMTDSavingsAccount.h"
#include <iostream>
#include <iomanip>
#include <string>
#include <sstream>


using namespace std;

JMTDCheckingAccount::JMTDCheckingAccount(double b, string a_n, double t_f){
    set_account_number(a_n);
    set_transaction_fee(t_f);
    set_balance(b);
}

And here is the parents header:

#ifndef JMTDSAVINGSACCOUNT_H
#define JMTDSAVINGSACCOUNT_H
#include <iostream>
#include <iomanip>
#include <string>
#include <sstream>


class JMTDSavingsAccount {
private:
    std::string account_number;
    double balance;

public:
    JMTDSavingsAccount(double balance, std::string account_number);
    void set_balance(double b);
    int get_balance() const;
    void set_account_number(std::string a_n);
    std::string get_account_number() const;
    virtual void deposit(double amount);
    virtual void withdraw(double amount);
    virtual std::string a_to_string();
};


#endif

and finally the parent source file:

#include <iostream>
#include <iomanip>
#include <string>
#include <sstream>
#include "JMTDSavingsAccount.h"

using namespace std;

JMTDSavingsAccount::JMTDSavingsAccount(double b, string a_n){
    set_balance(b);
    set_account_number(a_n);
}

void JMTDSavingsAccount::set_balance(double b){
    balance = b;
}

int JMTDSavingsAccount::get_balance() const{
    return balance;
}

void JMTDSavingsAccount::set_account_number(string a_n){
    account_number = a_n;
}

string JMTDSavingsAccount::get_account_number() const{
    return account_number;
}

void JMTDSavingsAccount::deposit(double amount){
    set_balance(balance + amount);
}

void JMTDSavingsAccount::withdraw(double amount){
    set_balance(balance - amount);
}

string JMTDSavingsAccount::a_to_string(){

}
  • 1
    https://stackoverflow.com/questions/2517050/c-construction-and-initialization-order-guarantees The base class constructor must be called in order to construct the derived class object. If you don't tell the derived class which base class constructor to use, it will attempt to use the default constructor, which doesn't exist for your base class. You can fix this by either 1. declaring and defining a default base class constructor, or 2. explicitly telling the derived class to use the base constructor of your choice in the initialization list. – JohnFilleau Feb 27 '20 at 15:50

1 Answers1

4

If you don't explicitly call a constructor of a base class it's default constructor will be called. But JMTDSavingsAccount has no default constructor, so you need to explicitly call it's constructor:

JMTDCheckingAccount::JMTDCheckingAccount(double b, string a_n, double t_f) :
    JMTDSavingsAccount(b, a_n) 
{
    set_account_number(a_n);
    set_transaction_fee(t_f);
    set_balance(b);
}

Check if the paramters match, I just picked b and a_n because their names match the names of the paramters in the constructors definition.

Lukas-T
  • 11,133
  • 3
  • 20
  • 30
  • 1
    I retracted my answer and voted for your much better answer. I was about to add a note about improving `JMTDSavingsAccount`s constructior to something like `JMTDSavingsAccount::JMTDSavingsAccount(double b, string a_n) : account_number(std::move(a_n)), balance(b) {}` though, so I'll leave it here instead. – Ted Lyngmo Feb 27 '20 at 15:59