2015-11-05 42 views
-1

看着类似的线程,这并没有显示出来。基本上我想要厨师继承雇员(基类)的功能和数据,但我与派生类的构造函数有问题。我得到的错误:没有匹配的函数调用'Employee :: 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 

回答

1

Employee()试图默认构造一个Employee,但没有Employee的默认构造函数。相反,使用你期望的构造函数的参数来构造它。

厨师构造函数应该是这样的:

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

注意构造函数体是空的。员工基类和成员变量在初始化列表中初始化为。没有转让必要的身体。您还应该更改基类构造函数,以便它使用初始化而不是赋值。

+0

很好的解释,如果可以的话,我会投票。感谢您的指导。 –

+0

如果答案是好的,请接受它。希望你能做到这一点。 :) –

相关问题