标准 POSIX 读取受到具有不同签名的读取方法的影响
我有一个带有读取功能的 C++ File 类,它应该将文件的全部内容(就像 Python 一样)读入缓冲区。 但是,当我尝试从 unistd.h 调用读取函数时,我得到:
file.cpp:21: 错误:没有匹配的函数可用于调用“File::read(int&, char*&, int)”
file.cpp:17: 注意:候选者是:char* File::read()
我究竟做错了什么? 它们具有完全不同的签名,为什么我不能简单地调用它?
I have a C++ File class with read function, that is supposed to read whole contents of a file (just like Python does) into a buffer. However, when I tried to call read function from unistd.h, I get:
file.cpp:21: error: no matching function for call to ‘File::read(int&, char*&, int)’
file.cpp:17: note: candidates are: char* File::read()
What am I doing wrong? These have completely different signatures, why can't I simply call it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您是否尝试过明确说明范围?
?
Have you tried being explicit about scope;
?
read 方法的 posix 标准版本定义为
extern "C"
。 这是必要的,这样读取的符号不会被 C++ 编译器破坏并链接到库中的正确函数。 混合和匹配 C 和 C++ 符号将产生不可预测的结果。 如果可能,请重命名 c++ 函数,以免与声明extern "C"
的任何符号发生冲突。The definition for the posix standard version of the read method is defined as
extern "C"
. This is neccesary so that the read symbol is not mangled by the C++ compiler and links against the proper function in the library. Mixing and matching C and C++ symbols will have unpredictable results. If possible, rename the c++ function so as not to conflict with any symbols that are declaredextern "C"
.