Question

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

class 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.

Was it helpful?

Solution

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){
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top