Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Friend declaration specifying a default argument must be a definition

Since updating to XCode 5.1 one of my projects now has that error in the title and will not build, I have changed architecture to 32-Bit as before, but still the same issue.

The line of code it refers to is;

friend float 
    DistBetweenModels (ShapeModel* pModel1, ShapeModel* pModel2,
                        enEvalType nEvalType = ET_EyeDist, enDistType nDistType = DT_Max); 

If I remove the 'friend' and leave the 'float' the project builds but I am not confident it is doing what it should.

like image 697
user3355723 Avatar asked Mar 20 '14 12:03

user3355723


People also ask

Which are the rules for default arguments?

Characteristics for defining the default argumentsThe values passed in the default arguments are not constant. These values can be overwritten if the value is passed to the function. If not, the previously declared value retains. During the calling of function, the values are copied from left to right.

What is a default argument how do you define one?

In computer programming, a default argument is an argument to a function that a programmer is not required to specify. In most programming languages, functions may take one or more arguments. Usually, each argument must be specified in full (this is the case in the C programming language).

What are the rules of default argument in C++?

Default Arguments in C++ A default argument is a value provided in a function declaration that is automatically assigned by the compiler if the calling function doesn't provide a value for the argument. In case any value is passed, the default value is overridden.

What can be used as a default function argument?

Default arguments are only allowed in the parameter lists of function declarations and lambda-expressions, (since C++11) and are not allowed in the declarations of pointers to functions, references to functions, or in typedef declarations.


1 Answers

If I remove the 'friend' and leave the 'float' the project builds but I am not confident it is doing what it should.

That is definitely not the correct thing to do.

This is the relevant issue.

A friend declaration with default arguments must also be a definition.

So you have some choices as to how to fix this. You can either move the definition of this function into the friend declaration:

friend float 
DistBetweenModels (ShapeModel* pModel1, ShapeModel* pModel2,
                    enEvalType nEvalType = ET_EyeDist, enDistType nDistType = DT_Max)
{
    // function definition goes here
}

Or you could remove the default arguments in the friend declaration:

friend float 
DistBetweenModels (ShapeModel* pModel1, ShapeModel* pModel2,
                    enEvalType nEvalType, enDistType nDistType);

But you should make sure that there's an earlier, non-friend declaration at namespace scope of this function that includes the default arguments.

I would choose the second solution; defining the function outside the class and moving the default arguments there. This is because there are some subtleties with name-lookup for friend functions which are defined inline. Inline friend functions should only be used for functions that are expected to be called via ADL (such as operator overloads).

This assumes that the function does need to be a friend. If not then you can just remove this friend declaration.

like image 94
bames53 Avatar answered Oct 26 '22 05:10

bames53