Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
816 views
in Technique[技术] by (71.8m points)

c++ - 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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...