简介:一般程序员都会比较懒,能赋值粘贴的就不手写(like me),但是头文件中的函数声明和源文件中的函数体定义的函数头,都有默认参数时就会报默认参数重定义的错误,一般情况下都是在头文件中定义默认参数,因为在调用函数的时候只需要看函数声明就知道参数表了。
- 代码结构如下
cpp文件中定义函数体;
//.cpp 文件
cv::Mat adaptiveThreshold(cv::Mat grayImage,
double maxval = 255,
int adaptiveMethod = cv::ADAPTIVE_THRESH_MEAN_C,
int thresholdType = cv::THRESH_BINARY_INV,
int blockSize = 11,
double C = 15)
{
if (grayImage.empty()) return grayImage;
//blockSize must be an odd number,like 3,5,7...
if (blockSize % 2 == 0) blockSize += 1;
if (grayImage.channels() == 3) cv::cvtColor(grayImage, grayImage, cv::COLOR_BGR2GRAY);
cv::adaptiveThreshold(grayImage, grayImage, maxval,adaptiveMethod,
thresholdType, blockSize,C);
return grayImage;
}
头文件中的函数声明如下
//.hpp文件
cv::Mat adaptiveThreshold(cv::Mat grayImage,
double maxval = 255,
int adaptiveMethod = cv::ADAPTIVE_THRESH_MEAN_C,
int thresholdType = cv::THRESH_BINARY_INV,
int blockSize = 11,
double C = 15);
- vs编译报错
imageprocess.cpp(111): error C2572: “adaptiveThreshold”: 重定义默认参数 : 参数 1
1>d:\mydoc\vs-proj\calculatedistance\calculatedistance\imageprocess.h(23): note: 参见“adaptiveThreshold”的声明
1>d:\mydoc\vs-proj\calculatedistance\calculatedistance\imageprocess.cpp(112): error C2572: “adaptiveThreshold”: 重定义默认参数 : 参数 2
1>d:\mydoc\vs-proj\calculatedistance\calculatedistance\imageprocess.h(23): note: 参见“adaptiveThreshold”的声明
1>d:\mydoc\vs-proj\calculatedistance\calculatedistance\imageprocess.cpp(113): error C2572: “adaptiveThreshold”: 重定义默认参数 : 参数 3
1>d:\mydoc\vs-proj\calculatedistance\calculatedistance\imageprocess.h(23): note: 参见“adaptiveThreshold”的声明
1>d:\mydoc\vs-proj\calculatedistance\calculatedistance\imageprocess.cpp(114): error C2572: “adaptiveThreshold”: 重定义默认参数 : 参数 4
1>d:\mydoc\vs-proj\calculatedistance\calculatedistance\imageprocess.h(23): note: 参见“adaptiveThreshold”的声明
1>d:\mydoc\vs-proj\calculatedistance\calculatedistance\imageprocess.cpp(115): error C2572: “adaptiveThreshold”: 重定义默认参数 : 参数 5
- 解决方法
把.cpp文件中的函数头中的默认参数去掉就可以了,r如下:
//.cpp 文件
cv::Mat adaptiveThreshold(cv::Mat grayImage,
double maxval ,
int adaptiveMethod ,
int thresholdType ,
int blockSize ,
double C )
{
if (grayImage.empty()) return grayImage;
//blockSize must be an odd number,like 3,5,7...
if (blockSize % 2 == 0) blockSize += 1;
if (grayImage.channels() == 3) cv::cvtColor(grayImage, grayImage, cv::COLOR_BGR2GRAY);
cv::adaptiveThreshold(grayImage, grayImage, maxval,adaptiveMethod,
thresholdType, blockSize,C);
return grayImage;
}