Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inherited Constructor not working | C++

My base class is located 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 the 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

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

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.

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 image 402
user3387140 Avatar asked Feb 12 '23 19:02

user3387140


2 Answers

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.

like image 194
Ben Voigt Avatar answered Feb 20 '23 08:02

Ben Voigt


According to the C++ Standard § 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)
{

}
like image 28
Vlad from Moscow Avatar answered Feb 20 '23 06:02

Vlad from Moscow