error: default argument given for parameter 1
I'm getting this error message with the code below:
c开发者_Go百科lass Money {
public:
Money(float amount, int moneyType);
string asString(bool shortVersion=true);
private:
float amount;
int moneyType;
};
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){
}
I made a similar error recently. this is how I resolved it.
when having a function prototype and definition. the default parameter is not specified in the definition.
eg:
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;
}
精彩评论