这是涉及的两个函数:
int FixedLengthRecordFile :: write (const int numRec, const FixedLengthFieldsRecord & rec)
{
/*** some code ***/
return rec.write(file); // FILE* file is an attribute of FixedLengthRecordFile
}
int FixedLengthFieldsRecord :: write (FILE* file) { /* ... code ... */ }
我得到这个错误:
FixedLengthRecordFile.cpp: In member function ‘int FixedLengthRecordFile::write(int, const FixedLengthFieldsRecord&)’:
FixedLengthRecordFile.cpp:211:23: error: no matching function for call to ‘FixedLengthFieldsRecord::write(FILE*&) const’
FixedLengthRecordFile.cpp:211:23: note: candidate is:
FixedLengthFieldsRecord.h:35:7: note: int FixedLengthFieldsRecord::write(FILE*) <near match>
FixedLengthFieldsRecord.h:35:7: note: no known conversion for implicit ‘this’ parameter from ‘const FixedLengthFieldsRecord*’ to ‘FixedLengthFieldsRecord*’
FixedLengthRecordFile.cpp:213:1: warning: control reaches end of non-void function [-Wreturn-type]
错误的原因是什么?我在代码中没有看到任何错误。此外,我还有另外两个类似的函数(write),它工作得很好。
最佳答案
int FixedLengthRecordFile::write( const int numRec,
const FixedLengthFieldsRecord& rec)
{
/*** some code ***/
return rec.write(file); // FILE* file is an attribute of FixedLengthRecordFile
}
int FixedLengthFieldsRecord::write(FILE* file)
您通过const
和const
引用传递参数,但是,您调用的函数rec.write(file)
不是const
函数,它可能会修改那些传入的对象,因此,编译器会报错。
您应该执行以下操作:
int FixedLengthFieldsRecord::write(FILE* file) const
// add const both declaration and definition ^^^
关于C++ 编译器错误 "no matching function",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15778384/