【发布时间】:2020-02-27 15:46:14
【问题描述】:
我不知道如何修复这个构造函数。它不断返回错误:“没有调用'JMTDSavingsAccount::JMTDSavingsAccount()'的匹配函数”这很奇怪,因为错误出现在子类的构造函数上(子类:JMTDCheckingAccount,父类:JMTDSavingsAccount)。构造函数不是没有传给子类吗?
子类的标题:
#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
子类的源文件:(仅我们正在处理的部分)
#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);
}
这里是父母的标题:
#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
最后是父源文件:
#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(){
}
【问题讨论】:
-
stackoverflow.com/questions/2517050/… 必须调用基类构造函数才能构造派生类对象。如果您不告诉派生类使用哪个基类构造函数,它将尝试使用默认构造函数,该构造函数在您的基类中不存在。您可以通过以下方式解决此问题:1. 声明和定义默认基类构造函数,或 2. 明确告诉派生类使用初始化列表中您选择的基类构造函数。
标签: c++