9

I'm trying to create a constructor with a default value. The complication comes from the use of separate header and code files for the class. I've got a header file that contains:

class foo {
    bool dbg;
    public:
        foo(bool debug = false);
}

And a code file containing:

foo::foo(bool debug = false) {
    dbg = debug;
}

When I try and compile with g++ (i.e. g++ -c foo.cc), it gives an error:

foo.cc:373:65: error: default argument given for parameter 1 of ‘foo::foo(bool)’
foo.h:66:4: error: after previous specification in ‘foo::foo(bool)’

What am I doing wrong?

Kiril Kirov
  • 37,467
  • 22
  • 115
  • 187
Clivest
  • 499
  • 1
  • 4
  • 14

5 Answers5

13

The default can only go in the header file. And using defaults in constructors (or other functions) is rarely a good idea, in my experience - it usually smacks of a kludge somewhere. Not to say there aren't a few in my own code!

  • Thanks. It works :). As you may be able to see from my example, I'm doing this so that some debug information can be provided if necessary. Is there a recommended way of implementing this, if not through default values in constructors? – Clivest May 28 '11 at 16:28
  • @nbt As opposed to constructor chaining? Especially when there are many members that need initialized? Seems to me this is one pragmatic benefit to default parameter values in constructors. – ybakos May 01 '12 at 11:56
8

Don't provide the default value in the definition:

foo::foo(bool debug) {
    dbg = debug;
}

Now its correct. Default value should be provided in the declaration only, which you've done in the header file.

By the way, prefer using member-initialization-list over assignments:


And of course, if its declaration-cum-definition, then you've to provide the default value (if you want it) right in the declaration-cum-definition:

class foo {
    bool dbg;
    public:
        foo(bool debug = false) : dbg(debug) {}
                               //^^^^^^^^^^^ using member initialization list
}
Community
  • 1
  • 1
Nawaz
  • 353,942
  • 115
  • 666
  • 851
4

The default value must be only in the declaration of the function, when the declaration and the definition are separated.


You could add the default value as an comment, if you like, but you should be aware, because changing the default value and forgetting to change the comment could cause some misleading (:

For example:

foo(bool debug = false);

//...

foo::foo(bool debug /* = false */ )
{ /* ... */ }
Kiril Kirov
  • 37,467
  • 22
  • 115
  • 187
2

In C++(I dont know of other languages), the default arguments are a part of only function declaration & not function definition.

class foo {
    bool dbg;
    public:
        foo(bool debug = false);
}

is fine, change your defintion to:

foo::foo(bool debug) {
    dbg = debug;
}
Alok Save
  • 202,538
  • 53
  • 430
  • 533
1

It doesn't need a default argument in member function definition,

foo::foo(bool debug) {
    dbg = debug;
}
cpx
  • 17,009
  • 20
  • 87
  • 142