poc*_*coa 83 c++ function member-functions default-arguments
我收到此错误消息,代码如下:
class Money {
public:
Money(float amount, int moneyType);
string asString(bool shortVersion=true);
private:
float amount;
int moneyType;
};
Run Code Online (Sandbox Code Playgroud)
首先,我认为默认参数不允许作为C++中的第一个参数,但允许使用.
Yac*_*oby 183
您可能正在重新定义函数实现中的默认参数.它应该只在函数声明中定义.
//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){
}
Run Code Online (Sandbox Code Playgroud)
小智 16
我最近犯了类似的错误。这就是我解决它的方法。
当有函数原型和定义时。定义中未指定默认参数。
例如:
int addto(int x, int y = 4);
int main(int argc, char** argv) {
int res = addto(5);
}
int addto(int x, int y) {
return x + y;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
72892 次 |
| 最近记录: |