views:

66

answers:

3

Here is my class definition:

class MyClass {
public:
   void test(int val = 0);
}

void MyClass::test(int val = 0) {
   //
}

When I try to compile this code I get the error: "default parameter given for parameter 1"

It's just a simple function, I don't know what's wrong. I'm using Eclipse + MinGW.

+1  A: 

There is no type specification of your variable. Is it intended?

Then you actually need to specify the default value inside the declaration, not where you implement it, it would be redundant.

Jack
Specifying both would be an *error*, not "redundant". In fact, this is exactly the error the OP is getting.
AndreyT
A: 

You don't need to ( and shouldn't ) repeat the default value in the function definition. It's only required in the declaration.

Troubadour
+1  A: 

Formally, you can specify the default argument wherever you want, but you can do it only once per parameter. Even if the value is the same, it has to be specificed either in the function declaration or in the definition, but not in both.

Of course, if the declaratuion is in the header file (and the definition is in implementation file), the common sense says that the default argument has to be specified in the header file, so that all translation units can "see" it.

AndreyT