error: default argument given for parameter 1

后端 未结 1 918
刺人心
刺人心 2020-12-04 14:57

I\'m getting this error message with the code below:

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


        
1条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-04 15:38

    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){
    }
    

    0 讨论(0)
提交回复
热议问题