简体   繁体   English

继承的构造函数不工作| C ++

[英]Inherited Constructor not working | C++

My base class is located in Employee.h and this is the code for the constructor. 我的基类位于Employee.h中,这是构造函数的代码。

Employee(string Fname = "First Name not Set.", 
         string Lname = "Last Name not Set.");

This is the code for the Employee.cpp 这是Employee.cpp的代码

Employee :: Employee(string Fname = "First Name not Set.", 
                     string Lname = "Last Name not Set.")
   : FirstName(Fname), LastName(Lname)
{

}

The problem are both my constructors and it says their parameters are wrong but I'm not sure what is wrong with them. 问题是我的构造函数,它说它们的参数是错误的,但我不确定它们有什么问题。

Manager.h Manager.h

class Manager: public Employee
public:
    Manager(string Fname = "First Name not Set.", 
            string Lname = "Last Name not Set.", double sal = 0.0,
            string BTitle = "Boss's Title not Set."): Employee (Fname,Lname){}

Manager.cpp Manager.cpp

Manager :: Manager(string Fname = "First Name not Set.", 
                   string Lname = "Last Name not Set.", double sal = 0.0,
                   string BTitle = "Boss's Title not Set."): Employee(Fname, Lname)
{
    FirstName = Fname;
    LastName = Lname;
    salary = sal;
    TitleOfBoss = BTitle;
}

This is error message I am getting: 这是我收到的错误消息:

'Manager::Manager' : redefinition of default parameter : parameter 4: : see declaration of 'Manager::Manager'
'Manager::Manager' : redefinition of default parameter : parameter 3: : see declaration of 'Manager::Manager'
'Manager::Manager' : redefinition of default parameter : parameter 2: : see declaration of 'Manager::Manager'
'Manager::Manager' : redefinition of default parameter : parameter 1: : see declaration of 'Manager::Manager'

Same thing with the Employee constructor. 与Employee构造函数相同。

error C2572: 'Employee::Employee' : redefinition of default parameter : parameter 2: see declaration of 'Employee::Employee'
error C2572: 'Employee::Employee' : redefinition of default parameter : parameter 1: see declaration of 'Employee::Employee'

Like the error message is telling you, you have defined the default parameter more than once. 就像错误消息告诉您的那样,您已经多次定义了默认参数。 It doesn't matter that the default value is the same in both cases; 在两种情况下,默认值都相同并不重要; it is still illegal. 它仍然是非法的。 The help page for that compiler error is pretty clear. 该编译器错误的帮助页面非常清楚。

Either the default parameters should be in the header file inside the class, where the constructor is declared, or they should be in the implementation of the constructor, but not both. 默认参数应该在类中的头文件中,声明构造函数,或者它们应该在构造函数的实现中,但不能同时在两者中。

I suggest that you leave them in the header, because default parameter values are part of the public interface. 我建议你将它们留在标题中,因为默认参数值是公共接口的一部分。 Then the constructor definition becomes: 然后构造函数定义变为:

Manager::Manager( /* default values provided in header */
                  string Fname  /* = "First Name not Set." */,
                  string Lname  /* = "Last Name not Set." */,
                  double sal    /* = 0.0 */,
                  string BTitle /* = "Boss's Title not Set." */)
   : Employee(Fname, Lname)
   , salary(sal), TitleOfBoss(BTitle)
{
}

The compiler will ignore the comments, they are just there to remind you the declaration is providing default parameters. 编译器将忽略注释,它们只是提醒您声明提供默认参数。

I also fixed your constructor to initialize subobjects using the initializer list. 我还修复了你的构造函数,使用初始化列表初始化子对象。 This isn't Java, it's very rare to have any code inside a constructor body. 这不是Java,在构造函数体中包含任何代码是非常罕见的。

According to the C++ Standard § 8.3.6/4: 根据C ++标准§8.3.6/ 4:

A default argument shall not be redefined by a later declaration (not even to the same value). 默认参数不应由后来的声明(甚至不是相同的值)重新定义。

However 然而

For non-template functions, default arguments can be added in later declarations of a function in the same scope. 对于非模板函数,可以在稍后的同一范围内的函数声明中添加默认参数。

So you could write either 所以你可以写

Employee(string Fname = "First Name not Set.", 
         string Lname = "Last Name not Set.");

//...

Employee :: Employee(string Fname, 
                     string Lname)
   : FirstName(Fname), LastName(Lname)
{

}

or 要么

Employee(string Fname, 
         string Lname);

//...

Employee :: Employee(string Fname = "First Name not Set.", 
                     string Lname = "Last Name not Set.")
   : FirstName(Fname), LastName(Lname)
{

}

or 要么

Employee(string Fname, 
         string Lname = "Last Name not Set.");

//...

Employee :: Employee(string Fname = "First Name not Set.", 
                     string Lname)
   : FirstName(Fname), LastName(Lname)
{

}

你已经为header和cpp中的每个参数定义了默认值(例如: string Fname = "First Name not Set." )从cpp文件中删除它们以解决冲突,如下所示:

Manager :: Manager(string Fname, string Lname, double sal, string BTitle)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM