首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >g++流匹配误差

g++流匹配误差
EN

Stack Overflow用户
提问于 2014-01-28 17:28:53
回答 2查看 1.3K关注 0票数 0
代码语言:javascript
复制
void FileIO :: openFile(const char* m_FileName,const char* m_FileMode);

我正在犯错误:

代码语言:javascript
复制
FileIO.cpp: In static member function ‘static void FileIO::openFile(const char*, const char*)’:
FileIO.cpp:12:45: error: no matching function for call to ‘std::basic_ifstream<char>::open(const char*&, const char*&)’
FileIO.cpp:12:45: note: candidate is:
In file included from FileIO.h:1:0:
/usr/include/c++/4.7/fstream:531:7: note: void std::basic_ifstream<_CharT, _Traits>::open(const char*, std::ios_base::openmode) [with _CharT = char; _Traits = std::char_traits<char>; std::ios_base::openmode = std::_Ios_Openmode]
/usr/include/c++/4.7/fstream:531:7: note:   no known conversion for argument 2 from ‘const char*’ to ‘std::ios_base::openmode {aka std::_Ios_Openmode}’
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-01-28 19:12:52

std::basic_ofstream::open不使用两个const char*。(注意:您的主题是ofstream,但从您的评论中可以看出您在谈论ifstream)。

ifstream/open

代码语言:javascript
复制
void open( const char *filename,
           ios_base::openmode mode = ios_base::in );
void open( const std::string &filename,                                  
           ios_base::openmode mode = ios_base::in ); (since C++11)

问题是第二个,而不是第一个论点。

代码语言:javascript
复制
ifstream ifs;
ifs.open("hello", "rb" /*<-- problem, this is a const char* not flags.*/);

相反,您需要传递std::ios_base标志。

代码语言:javascript
复制
ifstream ifs("hello", std::ios_base::in | std::ios_base::binary);

代码语言:javascript
复制
ifstream ifs;
ifs.open("hello", std::ios_base::in | std::ios_base::binary);

编辑

查看你在帖子后的评论(为什么你不编辑这篇文章?)您还试图检查“NULL”。

在C和C++中,'NULL‘是一个宏,它的#defined为0。因此,检查NULL可以检查空指针,但也可以测试数值。如果要检查文件是否已打开,则需要执行以下操作:

代码语言:javascript
复制
m_FileInput.open("hello", std::ios_base::in | std::ios_base::binary);
if (!m_FileInput.good()) // checks if the file opened.

如果可能,您应该尝试使用'nullptr‘而不是'NULL’。

票数 4
EN

Stack Overflow用户

发布于 2014-01-28 19:07:20

您正在尝试使用C的FILE*语法来调用C++打开函数。模式(读/写/追加)参数不是C++中的字符串文本,而是枚举值(可能是ORed )。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21412563

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档