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

时间:2014-10-14 03:46:54

标签: c++ class inheritance

我的基类位于Employee.h中,这是构造函数的代码。

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

这是Employee.cpp的代码

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

}

问题是我的构造函数,它说它们的参数是错误的,但我不确定它们有什么问题。

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 :: 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;
}

这是我收到的错误消息:

'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'

与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'

3 个答案:

答案 0 :(得分:4)

就像错误消息告诉您的那样,您已经多次定义了默认参数。在两种情况下,默认值都相同并不重要;它仍然是非法的。 The help page for that compiler error非常明确。

默认参数应该在类中的头文件中,声明构造函数,或者它们应该在构造函数的实现中,但不能同时在两者中。

我建议您将它们保留在标头中,因为默认参数值是公共接口的一部分。然后构造函数定义变为:

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)
{
}

编译器将忽略注释,它们只是提醒您声明提供默认参数。

我还修复了你的构造函数,使用初始化列表初始化子对象。这不是Java,在构造函数体中包含任何代码是非常罕见的。

答案 1 :(得分:4)

根据C ++标准§8.3.6/ 4:

  

默认参数不应由后来的声明重新定义(不是   即使是相同的价值)。

然而

  

对于非模板函数,可以在以后添加默认参数   在同一范围内声明函数。

所以你可以写

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

//...

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

}

Employee(string Fname, 
         string Lname);

//...

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

}

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

//...

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

}

答案 2 :(得分:0)

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

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