2
votes

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?

2
Are you sure your file has cpp not c extension ? - mikedu95
I'm sure, it's all C++. Although this error happened when porting from C to C++. The strange thing is that other functions from different header files work fine with default parameters. - Zimano
Couldn't duplicate: ideone.com/n3gK9i - erip
@erip I see, that's even stranger! - Zimano
Are you missing an include guard? Defining the same thing twice gives that error: ideone.com/9f2gtN - Inductiveload

2 Answers

4
votes

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 FOOand #define FOO at the top and #endif at the bottom. This worked.

Thanks to Inductiveload for the pointer!

4
votes

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.