Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error: default parameter given for parameter 1

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.

like image 358
pocoa Avatar asked Apr 13 '26 17:04

pocoa


1 Answers

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.

like image 78
AnT Avatar answered Apr 16 '26 07:04

AnT