The correct way to define default argument for a friend function in C++

不羁的心 提交于 2019-12-01 03:38:32

问题


I want to specify a default value for a friend function, as follows:

friend Matrix rot90 (const Matrix& a, int k = 1);

When compiling this line with Xcode 5.1.1, I get the following error

./Matrix.hh:156:19: error: friend declaration specifying a default argument must be a definition

What is the proper way of fixing it?

Thanks!


回答1:


The standard says (§8.3.6):

If a friend declaration specifies a default argument expression, that declaration shall be a definition and shall be the only declaration of the function or function template in the translation unit.

That is, if you specify the default argument on the friend declaration, you must also define the function right then and there. If you don't want to do that, remove the default argument there and add a separate declaration for the function that specifies the default arguments.

// forward declarations:
class Matrix;
Matrix rot90 (const Matrix& a, int k = 1);

class Matrix {
    friend Matrix rot90 (const Matrix&, int); //no default values here
};


来源:https://stackoverflow.com/questions/23333949/the-correct-way-to-define-default-argument-for-a-friend-function-in-c

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!