C ++ ifstream错误使用字符串作为打开文件路径。 [英] C++ ifstream error using string as opening file path.

查看:615
本文介绍了C ++ ifstream错误使用字符串作为打开文件路径。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有:

string filename: 
ifstream file(filename);

编译器提示ifstream文件和字符串之间没有匹配项。我需要将文件名转换为某个文件名吗?

The compilers complains about no match between ifstream file and a string. Do I need to convert filename to something?

这里是错误:

error: no matching function for call to ‘std::basic_ifstream<char, std::char_traits<char> >::basic_ifstream(std::string&)’
/usr/include/c++/4.4/fstream:454: note: candidates are: std::basic_ifstream<_CharT, _Traits>::basic_ifstream(const char*, std::_Ios_Openmode) [with _CharT = char, _Traits = std::char_traits<char>]


推荐答案

更改

ifstream file(filename);

ifstream file(filename.c_str());

因为 ifstream 的构造函数 const char * ,而不是 string pre-C ++ 11。

Because the constructor for an ifstream takes a const char*, not a string pre-C++11.

这篇关于C ++ ifstream错误使用字符串作为打开文件路径。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆