【问题标题】:constructor giving error: no matching function for call to [duplicate]构造函数给出错误:没有匹配的函数调用[重复]
【发布时间】: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++


【解决方案1】:

如果您没有显式调用基类的构造函数,则会调用其默认构造函数。但是JMTDSavingsAccount没有默认构造函数,所以需要显式调用它的构造函数:

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);
}

检查参数是否匹配,我刚刚选择了ba_n,因为它们的名称与构造函数定义中的参数名称匹配。

【讨论】:

  • 我撤回了我的答案并投票给了你更好的答案。我正准备添加一条关于将 JMTDSavingsAccounts 构造函数改进为 JMTDSavingsAccount::JMTDSavingsAccount(double b, string a_n) : account_number(std::move(a_n)), balance(b) {} 之类的注释,所以我将它留在这里。
猜你喜欢
  • 2020-06-09
  • 2014-10-11
  • 2012-05-09
  • 2013-07-23
  • 1970-01-01
  • 2015-09-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多