知道为什么在编译时会出现此错误吗?

ComplexNumber.cpp:21: error: default argument given for parameter 1 of ‘void ComplexNumber::print(std::ostream&) const’
ComplexNumber.h:17: error: after previous specification in ‘void ComplexNumber::print(std::ostream&) const’

这是我在某些特定区域的代码:

ComplexNumber.cpp
21    void ComplexNumber::print(ostream & out = cout) const {

ComplexNumber.h
17    void print(ostream & out = cout) const;

最佳答案

您只应在函数声明中(即在标题中)指定默认参数。您的实现应如下所示:

void ComplexNumber::print(ostream & out) const { ..... }

关于C++默认参数错误,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/10323587/

10-13 00:06