int CI2cHal::SetI2cSlaveAddress( UCHAR addr, bool force = false )
{
...
}
可以看到,我将参数“force”的默认值放在类源文件中,而不是类头文件中。
然后我在派生类中使用该函数,如下所示(派生类以公共方式继承基类):
SetI2cSlaveAddress( addr );
假设它将“force”参数视为“理所当然”的“false”。
然而,编译器(put in c++11 mode)抱怨并给出以下编译器错误:
/home/.../mystuff/domoproject/lib/i2cdevs/max6956io.cpp: In member function 'void CMax6956Io::Init(unsigned char, unsigned char, unsigned int)':
/home/.../mystuff/domoproject/lib/i2cdevs/max6956io.cpp:26:30: error: no matching function for call to 'CMax6956Io::SetI2cSlaveAddress(unsigned char&)'
/home/.../mystuff/domoproject/lib/i2cdevs/max6956io.cpp:26:30: note: candidate is:
In file included from /home/geertvc/mystuff/domoproject/lib/i2cdevs/../../include/i2cdevs/max6956io.h:35:0,
from /home/geertvc/mystuff/domoproject/lib/i2cdevs/max6956io.cpp:1:
/home/.../mystuff/domoproject/lib/i2cdevs/../../include/i2chal/i2chal.h:65:9: note: int CI2cHal::SetI2cSlaveAddress(unsigned char, bool)
/home/.../mystuff/domoproject/lib/i2cdevs/../../include/i2chal/i2chal.h:65:9: note: candidate expects 2 arguments, 1 provided
make[2]: *** [lib/i2cdevs/CMakeFiles/i2cdevs.dir/max6956io.cpp.o] Error 1
make[1]: *** [lib/i2cdevs/CMakeFiles/i2cdevs.dir/all] Error 2
make: *** [all] Error 2
但是当我在基类的< em >头< / em >文件中添加默认参数时:
int SetI2cSlaveAddress( UCHAR addr, bool force = false );
并从基类的源文件中删除它:
int CI2cHal::SetI2cSlaveAddress( UCHAR addr, bool force )