error:为参数1给出的默认参数 [英] error: default argument given for parameter 1
本文介绍了error:为参数1给出的默认参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我收到以下错误讯息:
class Money {
public:
Money(float amount, int moneyType);
string asString(bool shortVersion=true);
private:
float amount;
int moneyType;
};
首先我认为默认参数不允许作为C ++中的第一个参数,但允许。 / p>
First I thought that default parameters are not allowed as a first parameter in C++ but it is allowed.
推荐答案
您可能正在重新定义函数实现中的默认参数。它只应在函数声明中定义。
You are probably redefining the default parameter in the implementation of the function. It should only be defined in the function declaration.
//bad (this won't compile)
string Money::asString(bool shortVersion=true){
}
//good (The default parameter is commented out, but you can remove it totally)
string Money::asString(bool shortVersion /*=true*/){
}
//also fine, but maybe less clear as the commented out default parameter is removed
string Money::asString(bool shortVersion){
}
这篇关于error:为参数1给出的默认参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文