Inherited Constructor not working | C ++

My base class is in Employee.h and this is the code for the constructor.

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

This is the code for Employee.cpp

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

}

The problem lies in my constructors, and he says that their parameters are incorrect, but I'm not sure what is wrong with them.

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 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 Title not Set."): Employee(Fname, Lname)
{
    FirstName = Fname;
    LastName = Lname;
    salary = sal;
    TitleOfBoss = BTitle;
}

This is the error message I get:

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

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'
+4
source share
3 answers

Like the error message, you specified the default parameter more than once. It does not matter that the default value is the same in both cases; it is still illegal. The help page for this compiler error is pretty straightforward.

, , , .

, . :

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 Title not Set." */)
   : Employee(Fname, Lname)
   , salary(sal), TitleOfBoss(BTitle)
{
}

, , , .

, . Java, - .

+4

++ § 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)
{

}
+4

You have defined default values ​​for each parameter in both the header and cpp (for example:) string Fname = "First Name not Set."Remove them from the cpp file to resolve the conflict as follows:

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

All Articles