error: default argument given for parameter 1


class Money {
public:
    Money(float amount, int moneyType);
    string asString(bool shortVersion=true);
private:
    float amount;
    int moneyType;};

如上图的红色代码。会出现 e rror: default argument given for parameter 1 错误,这是因为有默认参数的函数在声明和定义的时候有点小坑。
正确的做法是:1.在声明的同时就定义。
                       2.先声明再定义的情况下,定义的时候需要:
// 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){}
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值