c++ - 错误 : default argument given for parameter 1

标签 c++ function default-arguments

我收到此错误消息,代码如下:

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

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

最佳答案

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

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

关于c++ - 错误 : default argument given for parameter 1,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2545720/

相关文章:

c++ - 我是否错误地使用了默认参数?

c++ - 多个动态链接库(DLL)是否可以从静态库(LIB)共享线程本地存储

php - 罗马数字转整数函数

C++:仅在一个元素上使用 unique_copy

python - 在另一个函数中为生成器调用 yield

node.js - 如何在 Node.js 中的异步函数上正确使用 Promise?

c++ - 重载运算符 delete 可以有默认参数吗?

c++ - 使函数参数默认为周围范围

c++ - pair(const std::pair<_T1, _T2>&) 被隐式删除,因为默认定义格式不正确错误:分配 unique_ptr 映射时

c++ - C 枚举不同的编译器