错误:调用"员工::员工()"没有匹配函数

error: no matching function for call to 'Employee::Employee()'

本文关键字:员工 函数 调用 错误      更新时间:2023-10-16

查看了类似的线程,但没有显示出来。基本上,我希望chef从employee(基类)继承函数和数据,但派生类的构造函数有问题。我得到了一个错误:对"Employee::Employee()"的调用没有匹配的函数。有人能告诉我如何声明这个派生类的构造函数和这个程序未来的派生类吗。尝试了很多事情,但似乎都没能成功。

class Employee
{
    public:
        Employee(int theempID, string thefirstName, string thelastName, char theempClass, int thesalary)
    {
        this->empID = theempID;
        this->firstName = thefirstName;
        this->lastName = thelastName;
        this->empClass = theempClass;
        this->salary = thesalary;
    };

protected:
    int empID;
    string firstName;
    string lastName;
    char empClass;
    int salary;
};

class Chef : public Employee
{
    public:
        Chef(int theempID, string thefirstName, string thelastName, char theempClass, int thesalary, string theempCuisine) : Employee() {}
    {
        this->empID = theempID;
        this->firstName = thefirstName;
        this->lastName = thelastName;
        this->empClass = theempClass;
        this->salary = thesalary;
        this->empCuisine = theempCuisine;
    };
    string getCuisine()
    {
        return empCuisine;
    }
protected:
    string empCuisine;
};

#endif // EMPLOYEE

Employee()正在尝试默认构造Employees,但没有Employee的默认构造函数。相反,使用构造函数所期望的参数来构造它。

Chef构造函数应该是这样的:

Chef(int theempID, string thefirstName, string thelastName, char theempClass, int thesalary, string theempCuisine) :
    Employee(theempID, thefirstName, thelastName, theempClass, thesalary), empCuisine(theempCuisine)
    {}

请注意,构造函数的主体为空。Employee基类和成员变量在初始化列表中被初始化。正文中不需要赋值。您还应该更改基类构造函数,使其使用初始化而不是赋值。