C++ 编译器错误 "no matching function"

标签 c++ compiler-errors

这是涉及的两个函数:

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) 

您通过constconst 引用传递参数,但是,您调用的函数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/

相关文章:

c++ - 通过统一缓冲区对象发送到 GLSL 的数据泄漏到其他绘制调用中 (OpenGL 3.2)

c++ - 冒号的含义

c++ - 如何将 mouseWheelEvent 发送到 SWF,加载到 QWebView

ios - 使用文件 md5.c 编译 Xcode 时的警告

generics - 未为类型`x`实现特征'x`

python-2.7 - 为什么是TypeError : unhashable type: 'list' coming up?

C++ 将全局列表传递给线程

c++ - 如何使用 Visual C++ 中的 Delphi 寄存器调用约定调用函数?

c - 仅当参数不是常量时,math.h 中的 sqrt 才会导致链接器错误 "undefined reference to sqrt"

python - 为什么 Python 编译这段代码没有抛出错误?