4

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'
  • This is essentially the same question as http://stackoverflow.com/questions/4989483/where-to-put-default-parameter-value-in-c – Chris Culter Oct 14 '14 at 03:52
  • 1
    Same here: [The compiler is complaining about my default parameters?](https://stackoverflow.com/questions/6210450/the-compiler-is-complaining-about-my-default-parameters) –  Oct 14 '14 at 03:53
  • @remyabel: Except for the fact that the code in that question doesn't reproduce the error OP claims to get :( This question has a self-contained snippet of code that produces the problem. Well, except for missing `{};` around the class and missing member declarations and ... But the relevant parts are shown here. – Ben Voigt Oct 14 '14 at 03:57

3 Answers3

4

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.

Ben Voigt
  • 277,958
  • 43
  • 419
  • 720
  • It says FirstName and LastName are nonstatic data member or base class of "manager". FirstName and LastName are from the base class. – user3387140 Oct 14 '14 at 04:00
  • @user3387140: Yes, realized that, and fixed it by uncommenting your base class constructor call. Completely separate problem from the default parameters. – Ben Voigt Oct 14 '14 at 04:00
4

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

}
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
0

You've defined defaults for each of the parameters in both the header and the cpp (eg: string Fname = "First Name not Set.") Remove them from the cpp file to resolve the conflict like so:

Manager :: Manager(string Fname, string Lname, double sal, string BTitle)
JBY
  • 238
  • 2
  • 10