gpt4 book ai didi

c++ - 没有匹配的函数 - ifstream open()

转载 作者:太空宇宙 更新时间:2023-11-04 03:39:24 27 4
gpt4 key购买 nike

这是有错误的代码部分:

std::vector<int> loadNumbersFromFile(std::string name)
{
std::vector<int> numbers;

std::ifstream file;
file.open(name); // the error is here
if(!file) {
std::cout << "\nError\n\n";
exit(EXIT_FAILURE);
}

int current;
while(file >> current) {
numbers.push_back(current);
file.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}
return numbers;
}

好吧,我有点不知道发生了什么。整个事情在 VS 中编译正确。不过我需要用 dev cpp 来编译它。

我注释掉了上面代码中抛出错误的行。错误是:

no matching function for call 'std::basic_ifstream<char>::open(std::string&)
no matching function for call 'std::basic_ofstream<char>::open(std::string&)
<小时/>

在代码的不同部分,我收到诸如 numeric_limits 不是 std 的成员max() 尚未声明之类的错误,尽管它们存在于 iostream 类中并且一切都可以在 VS 中运行。

<小时/>

为什么我会收到此错误?

最佳答案

更改为:

file.open(name.c_str());

或者只使用构造函数,因为没有理由将构造和打开分开:

std::ifstream file(name.c_str());

支持std::string argument已在 c++11 中添加。

由于 loadNumbersFromFile() 不会修改由 std::string const& 传递的参数来记录该事实并避免不必要的复制。

关于c++ - 没有匹配的函数 - ifstream open(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30380203/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com