简体   繁体   中英

Default parameter given for parameter X of function Y

I am trying to assign a default value to a parameter in this function declaration:

bool gatt_db_service_set_active(struct gatt_db_attribute *attrib, bool active, int fd=0);

However, I am getting error

default argument given for parameter 3 of 'bool gatt_db_service_set_active(gatt_db_attribute*, bool, int)' [-fpermissive]

Then it says:

previous specification in 'bool gatt_db_service_set_active(gatt_db_attribute*, bool, int)' here: bool gatt_db_service_set_active(struct gatt_db_attribute *attrib, bool active, int fd;<

Which points to the same function declaration.

This is the definition:

bool gatt_db_service_set_active(struct gatt_db_attribute *attrib, bool active, int fd) 
{
    //stuff
}

As you can see I did not set the default parameter twice, as is the problem with most questions regarding this error. I am compiling this with gcc version 5.2.1 on Ubuntu 15.1

Does anyone know what is happening here?

It turned out that there were no header guards in the header where I was declaring the function.

I found it strange the error was pointing to itself, so I tried to solve it by simply adding a header guard like #ifndef FOO and #define FOO at the top and #endif at the bottom. This worked.

Thanks to Inductiveload for the pointer!

If you have somehow declared the function twice (so that the compiler sees two lines like this):

bool gatt_db_service_set_active(struct gatt_db_attribute *attrib, bool active, int fd=0);

Then you will see this error, because there are two declarations with a default, which is not allowed by the C++ standard (section 8.3.6, para 4):

... A default argument shall not be redefined by a later declaration.

This example code demonstrates the error. Note that you can multiply-declare a function if you don't redefine the defaults, which might be useful for a forward declaration.

This could be because of a missing include guard, in which case the compiler will tell you both declarations are on the same line. You might have this situation in the following case:

// header.h
// No include guard!
bool gatt_db_service_set_active(struct gatt_db_attribute *attrib, bool active, int fd=0);
// ^^ compiler error on this line

// otherheader.h
#include "header.h"

// impl.cpp
#include "header.h"
#include "otherheader.h"

void main()
{} 

The solution is an include guard like this:

// header.h
#ifndef HEADER__H_
#define HEADER__H_

bool gatt_db_service_set_active(struct gatt_db_attribute *attrib, bool active, int fd=0);

#endif // HEADER__H_

This will prevent the second (and subsequent) inclusions of header.h declaring the same thing twice.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM