C++ 将函数中的参数初始化为零。但是在构建代码时仍然有一个错误
可能重复:C++ 将函数中的参数初始化为零。但是在构建代码时仍然有一个错误,c++,function,g++,overloading,C++,Function,G++,Overloading,可能重复: 错误: **** Internal Builder is used for build **** g++ -O0 -g3 -Wall -c -fmessage-length=0 -o src\Calculator.o ..\src\Calculator.cpp ..\src\Calculator.cpp:26:55: error: default argument given for parameter 1 of 'CComplex::CComplex
错误:
**** Internal Builder is used for build ****
g++ -O0 -g3 -Wall -c -fmessage-length=0 -o src\Calculator.o ..\src\Calculator.cpp
..\src\Calculator.cpp:26:55: error: default argument given for parameter 1 of 'CComplex::CComplex(float, float)'
..\src\/Calculator.h:25:9: error: after previous specification in 'CComplex::CComplex(float, float)'
..\src\Calculator.cpp:26:55: error: default argument given for parameter 2 of 'CComplex::CComplex(float, float)'
..\src\/Calculator.h:25:9: error: after previous specification in 'CComplex::CComplex(float, float)'
Build error occurred, build is stopped
Time consumed: 563 ms.
有没有人遇到过类似的问题。可能的解决方法是什么?函数定义中的默认参数是错误的
class {
void CComplex(float a=0.0, floatb=0.0);
};
如果您有这样的函数定义,那么它是错误的:
void CComplex::CComplex(float a=0.0, floatb=0.0)
{
}
应该是:
void CComplex(float a, float)
{
}
然后
您需要在函数声明(通常在头文件中)中声明函数参数的默认值,但不需要声明定义(通常在cpp文件中)。因此,在您的情况下,代码应该如下所示: 在.h文件中
CComplex(float r=0.0, float i=0.0);
在.cpp文件中
CComplex::CComplex(float r, float i)
{
// ...
}
我猜您在函数定义中指定了默认参数;你不应该。查看代码会有帮助。看起来需要查看的是
Calculator.h
和Calculator.cpp
中构造函数的声明。你能发布代码吗?我打赌它是classccomplex{CComplex(float r,float I=0.0);}分别是code>和CComplex::CComplex(float r,float i=0.0){}
。您回答了一些完全不同的问题,然后编辑了您的问题以匹配我的。。。但如果我像上面那样声明,我就无法将第二个参数传递给set函数CComplex(浮点实值=0,浮点虚值=0);//声明//无效集的默认值(浮点实型、浮点虚型);CComplex(CComplex const&c);浮动abs();作废打印();CComplex&operator++();c复合运算符++(int)//
CComplex::CComplex(float r, float i)
{
// ...
}