This question already has an answer here:
No matching function - ifstream open()
(1个答案)
5年前关闭。
我不确定如何修复错误(如下)。有人能帮我吗?
顺便说一句:它可以在VS2012上运行,但是当我在linux终端上运行时,它给了我这个错误
错误:
Librarian.cpp:
(1个答案)
5年前关闭。
我不确定如何修复错误(如下)。有人能帮我吗?
顺便说一句:它可以在VS2012上运行,但是当我在linux终端上运行时,它给了我这个错误
错误:
Librarian.cpp:
bool Librarian::addBooks(string file)
{
ifstream infile(file);
if (!infile)
{
cerr << "File could not be opened." << endl;
return false;
}
for (;;) {
char s[MAX];
infile.getline(s, MAX);
if (infile.eof()) break;
cout << s << endl;
}
return true;
}
最佳答案
根据std::basic_ifstream,构造函数直到C++ 11才采用string&
。在C++ 11之前,它只需要const char *
。解决您的问题最简单的方法是:
ifstream infile(file.c_str());
std::string::c_str()
获取字符串的基础char指针,以便可以在构造函数中使用。或者,您可以按照注释中的建议使用C++ 11,但它取决于您的编译器版本(看起来您的编译器不支持它)。关于c++ - 如何修复没有匹配功能的ifstream错误? ,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/22567256/