web-dev-qa-db-ja.com

エラー:パラメータ1に指定されたデフォルト引数

次のコードでこのエラーメッセージが表示されます。

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

最初に、デフォルトパラメータはC++の最初のパラメータとして許可されていませんが、許可されていると考えました。

82
pocoa

おそらく、関数の実装でデフォルトのパラメーターを再定義しているでしょう。関数宣言でのみ定義する必要があります。

//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){
}
179
Yacoby