2010-03-30 110 views
60

我收到此错误信息与下面的代码中给出默认参数:错误:参数1

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

首先,我认为默认参数不允许作为在C++中的第一参数,但它是允许的。

+0

你能否提供更多的细节? – 2010-03-30 13:55:15

+0

你使用什么编译器? – 2010-03-30 13:55:31

+0

我在Windows上使用Eclipse CDT和MinGW 5.1.6。 – pocoa 2010-03-30 14:00:54

回答

134

您可能重新定义了函数实现中的默认参数。它只应在函数声明中定义。

//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

现在它说: string Money :: asString()'与'Money'类中的任何一个不匹配 – pocoa 2010-03-30 13:58:54

+1

@pocoa你仍然需要保持'bool shortVersion'参数,只是删除或注释掉'= true' – Yacoby 2010-03-30 14:01:31

+0

@雅各比:谢谢,你是对的。它没有任何意义,很混乱。 – pocoa 2010-03-30 14:09:12

相关问题