void FileIO :: openFile(const char* m_FileName,const char* m_FileMode);我正在犯错误:
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}’发布于 2014-01-28 19:12:52
std::basic_ofstream::open不使用两个const char*。(注意:您的主题是ofstream,但从您的评论中可以看出您在谈论ifstream)。
ifstream/open
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)问题是第二个,而不是第一个论点。
ifstream ifs;
ifs.open("hello", "rb" /*<-- problem, this is a const char* not flags.*/);相反,您需要传递std::ios_base标志。
ifstream ifs("hello", std::ios_base::in | std::ios_base::binary);或
ifstream ifs;
ifs.open("hello", std::ios_base::in | std::ios_base::binary);编辑
查看你在帖子后的评论(为什么你不编辑这篇文章?)您还试图检查“NULL”。
在C和C++中,'NULL‘是一个宏,它的#defined为0。因此,检查NULL可以检查空指针,但也可以测试数值。如果要检查文件是否已打开,则需要执行以下操作:
m_FileInput.open("hello", std::ios_base::in | std::ios_base::binary);
if (!m_FileInput.good()) // checks if the file opened.如果可能,您应该尝试使用'nullptr‘而不是'NULL’。
发布于 2014-01-28 19:07:20
您正在尝试使用C的FILE*语法来调用C++打开函数。模式(读/写/追加)参数不是C++中的字符串文本,而是枚举值(可能是ORed )。
https://stackoverflow.com/questions/21412563
复制相似问题