C++编译器错误"no matching function"
C++ compiler error "no matching function"
这是涉及到的两个函数:
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 ^^^
让我们看看错误消息:
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*’
它说它不能从const FixedLengthFieldsRecord*
转换到FixedLengthFieldsRecord*
这是一个很好的提示。
在下一行中,rec
是const引用,
return rec.write(file); // FILE* file is an attribute of FixedLengthRecordFile
但是下面的函数是NOT const
qualified
int FixedLengthFieldsRecord :: write (FILE* file) { /* ... code ... */ }
这就是问题所在!
有(至少)两种解决方案:
1)将rec
更改为非const
引用
2)修改write()
方法的签名为const
合格
选项#2是首选方法。
相关文章:
- "error: no matching function for call to"构造函数错误
- 调用专用模板时出错"no matching function for call to [...]"
- "no matching function for call to 'Vector::Vector'"错误
- 我在 ifstream input_file(文件名)行中收到错误"no matching function to call";
- 为什么当我在 std::string 中打开文件名的 fstream 时出现"no matching function"错误?
- 从 TVector2 类派生复杂类:"No matching function for call to Complex::Complex()"
- 学习 c++,编译错误"no matching function for call"
- C++ "error: no matching function"
- 结构错误:"no matching function for call to "
- 创建函数变体向量时"No matching function for call"错误
- 编译时二叉搜索树错误的反向迭代器表示"no matching function call for operator=()"
- 如果子类中没有构造函数方法,则错误"no matching function for call to 'LGame::LGame(String&)'"
- 提升多精度浮点数128:标准::exp错误:'no matching function for call'
- "error: no matching function for call to",我无法更改结构的定义
- 替换派生对象向量中的对象"no matching function to call"
- 我的程序显示许多错误,"error: no matching function for call to 'getPercentScore'"
- 调用模板函数的问题"No matching function for call"参数:迭代器、对象函数
- 尝试在矢量上使用擦除功能时"no matching function for call"
- std::<int>betterer() 比较器与 partial_copy_sort 的困难,在 Mac OSX 上"no matching function call.."
- "error: no matching function for call to ‘push_back(char [6])" .如何解决此代码中的错误?