c++ - 我在 ifstream input_file(filename); 行中收到错误“没有要调用的匹配函数”;
问题描述
int main(int argc, const char * argv[]) {
cout << "Give the name of the input file : ";
string filename;
cin >> filename;
cout << "Reading from " << filename << "..." << endl;
ifstream input_file(filename);
if( !input_file ) {
// stuff...
}
}
解决方案
假设您有#include <fstream>
并且using namespace std;
在您的代码中,然后更改此:
ifstream input_file(filename);
对此:
ifstream input_file(filename.c_str());
在您的编译器版本的 C++ 中,std::ifstream
构造函数仅将 aconst char*
作为输入,而不是 a std::string
。需要 C++11 才能按std::string
原样传递。如果你的编译器支持 C++11,可能你没有启用它。检查编译器的文档。
推荐阅读
- semantic-ui-react - Semantic-ui-react 输入字段自动提交触发错误行为
- javascript - 如何使用 JQuery 在 MVC 中删除选中的行
- c++ - 是否可以在不运行 TensorFlow 图的情况下使用 TensorFlow C++ API 进行形状推断?
- sql - 在动态 SQL 查询中返回 To 和 From 日期之间的数据
- spring-boot - 控制器spring boot中的父子关系
- excel - 公式返回 true,但不会发生格式化
- java - Spring Framework 和 rpmbuild:无法获取用于条目的嵌套存档
- facebook - Facebook 应用审核无法生成页面访问令牌
- javascript - 如何从 JS 中的 JSON 调用具有本地路径的图像?
- python - Apache 超集主题和样式