看着类似的线程,这并没有显示出来。基本上我希望大厨继承员工(基类)的功能和数据,但是我对派生类的构造函数有疑问。我收到错误:没有匹配函数来调用' 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
答案 0 :(得分:1)
Employee()正在尝试默认构造一个Employee,但Employee没有默认构造函数。相反,使用您期望的构造函数构造它。
Chef构造函数应如下所示:
Chef(int theempID, string thefirstName, string thelastName, char theempClass, int thesalary, string theempCuisine) :
Employee(theempID, thefirstName, thelastName, theempClass, thesalary), empCuisine(theempCuisine)
{}
注意构造函数的主体是空的。初始化列表中的Employee基类和成员变量是初始化。正文中不需要赋值。您还应该更改基类构造函数,以便它使用初始化而不是赋值。