我有一个名为 TeamLeader , ProductionWorker 和员工
的课程ProductionWorker 扩展了班级员工。
TeamLeader 扩展 ProductionWorker 。 有问题的构造函数如下:
TeamLeader :: TeamLeader(int trainingHoursCompleted, int shift, double hourlyPayRate) : ProductionWorker(shift, hourlyPayRate){
monthlyBonus = 1000;
requiredTrainingHours = 20;
this->trainingHoursCompleted = trainingHoursCompleted;
}
错误内容如下:没有匹配的构造函数用于初始化 ' ProductionWorker'
... shift,double hourlyPayRate):ProductionWorker(shift,hourlyPayRate)。
我在课程 ProductionWorker 中的构造函数如下:
ProductionWorker :: ProductionWorker() : Employee(){
shift = 0;
hourlyPayRate = 0;
}
ProductionWorker :: ProductionWorker(int shift, double hourlyPayRate, string employeeName, string hireDate, int employeeNumber) : Employee(employeeName, hireDate, employeeNumber) {
this->shift = shift;
this->hourlyPayRate = hourlyPayRate;
}
如果我添加"缺少"有问题的 TeamLeader 构造函数的参数如此
TeamLeader :: TeamLeader(int trainingHoursCompleted, int shift, double hourlyPayRate) : ProductionWorker(shift, hourlyPayRate, employeeName, hireDate, employeeNumber){
monthlyBonus = 1000;
requiredTrainingHours = 20;
this->trainingHoursCompleted = trainingHoursCompleted;
}
我收到以下错误: TeamLeader.cpp:23:128:错误:' employeeName'是' Employee'
的私人会员对于 TeamLeader
无法访问的其他两个参数,也会发生此错误谁能告诉我如何解决这个问题?因为如果感觉像一个永无止境的圈子......
TeamLeader.cpp
#include <stdio.h>
#include <String>
#include "TeamLeader.h"
using namespace std;
TeamLeader :: TeamLeader() : ProductionWorker(){
monthlyBonus = 1000;
requiredTrainingHours = 20;
trainingHoursCompleted = 0;
}
TeamLeader :: TeamLeader(int trainingHoursCompleted, int shift, double hourlyPayRate) : ProductionWorker(shift, hourlyPayRate){
monthlyBonus = 1000;
requiredTrainingHours = 20;
this->trainingHoursCompleted = trainingHoursCompleted;
}
void TeamLeader :: setTrainingHoursCompleted(int trainingHoursCompleted){
this->trainingHoursCompleted = trainingHoursCompleted;
}
ProductionWorker.cpp
#include "ProductionWorker.h"
ProductionWorker :: ProductionWorker() : Employee(){
shift = 0;
hourlyPayRate = 0;
}
ProductionWorker :: ProductionWorker(int shift, double hourlyPayRate, string employeeName, string hireDate, int employeeNumber) : Employee(employeeName, hireDate, employeeNumber) {
this->shift = shift;
this->hourlyPayRate = hourlyPayRate;
}
void ProductionWorker :: setShift(int shift){
this->shift = shift;
}
void ProductionWorker :: setHourlyPayRate(double hourlyPayRate){
this->hourlyPayRate = hourlyPayRate;
}
Employee.cpp
Employee :: Employee(){
employeeName = "NO NAME ENTERED";
hireDate = "NO DATE ENTERED";
employeeNumber = 0;
}
Employee :: Employee(string employeeName, string hireDate, int employeeNumber){
this->employeeName = employeeName;
this->hireDate = hireDate;
this->employeeNumber = employeeNumber;
}
void Employee :: setEmployeeName(string employeeName){
this->employeeName = employeeName;
}
void Employee :: setHireDate(string hireDate){
this->hireDate = hireDate;
}
void Employee :: setEmployeeNumber(int employeeNumber){
this->employeeNumber = employeeNumber;
}
答案 0 :(得分:1)
如果您希望团队负责人拥有姓名等,那么TeamLeader
构造函数必须接受名称:
TeamLeader :: TeamLeader(int trainingHoursCompleted, int shift, double hourlyPayRate, string employeeName, string hireDate, int employeeNumber)
: ProductionWorker(shift, hourlyPayRate, employeeName, hireDate, employeeNumber)
, monthlyBonus(1000), requiredTrainingHours(20)
, trainingHoursCompleted(trainingHoursCompleted)
{ }
注意:最好使用构造函数初始化列表,而不是类体内的赋值语句。
如果您希望团队负责人没有姓名(虽然我不知道您将如何在此方法中设置名称)并且: ProductionWorker(shift, hourlyPayRate)
工作,那么您需要添加一个构造函数给{ {1}}有两个参数,例如:
ProductionWorker
NB。此答案假定ProductionWorker :: ProductionWorker(int shift, double hourlyPayRate) :
shift(shift), hourlyPayRate(hourlyPayRate)
{ }
和shift
是hourlyPayRate
的成员变量。
如果您使用的是C ++ 11,那么您可以使用委派构造函数来避免重复这么多。另外,查看默认参数。