首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >我无法在不违反c++扩展规则的情况下创建匹配的构造函数

我无法在不违反c++扩展规则的情况下创建匹配的构造函数
EN

Stack Overflow用户
提问于 2015-04-13 01:10:19
回答 1查看 186关注 0票数 0

我有一个名为ProductionWorker TeamLeaderEmployee的类

ProductionWorker扩展类Employee

扩展ProductionWorker.的TeamLeader所述的构造函数如下:

代码语言:javascript
复制
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类中的构造函数如下:

代码语言:javascript
复制
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构造函数中,如下所示

代码语言:javascript
复制
    TeamLeader :: TeamLeader(int trainingHoursCompleted, int shift, double hourlyPayRate) : ProductionWorker(shift, hourlyPayRate, employeeName, hireDate, employeeNumber){
monthlyBonus = 1000;
requiredTrainingHours = 20;
this->trainingHoursCompleted = trainingHoursCompleted;

}

I得到以下错误: TeamLeader.cpp:23:128: error:'employeeName‘是'Employee’的私有成员

对于TeamLeader无法访问的其他两个参数,也会发生此错误。

有人能告诉我怎么解决这个问题吗?因为如果感觉像是一个永无止境的圆圈..。

TeamLeader.cpp

代码语言:javascript
复制
#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

代码语言:javascript
复制
#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

代码语言:javascript
复制
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;
}
EN

回答 1

Stack Overflow用户

发布于 2015-04-13 02:13:35

如果您希望团队负责人有一个名称等等,那么TeamLeader构造函数必须接受这个名称:

代码语言:javascript
复制
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)工作,那么您需要向ProductionWorker添加一个构造函数,该构造函数包含两个参数,例如:

代码语言:javascript
复制
ProductionWorker :: ProductionWorker(int shift, double hourlyPayRate) : 
    shift(shift), hourlyPayRate(hourlyPayRate)
{ }

注意:这个答案假设shifthourlyPayRateProductionWorker的成员变量。

如果您使用的是C++11,那么您可以使用委托构造函数来避免重复多次。此外,查看默认参数。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29596524

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档