没有匹配的功能 - ifstream open()

use*_*786 17 c++ string iostream

这是代码中包含错误的部分:

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;
}
Run Code Online (Sandbox Code Playgroud)

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

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

调用'std :: basic_ifstream :: open(std :: string&)
没有匹配函数调用'std :: basic_ofstream :: open(std :: string&)没有匹配函数


在代码的不同部分,我得到的错误如'numeric_limits不是std的成员'或'max()尚未声明',尽管它们存在于iostream类中,并且一切都在VS中工作.


为什么我收到此错误?

hmj*_*mjd 40

改成:

file.open(name.c_str());
Run Code Online (Sandbox Code Playgroud)

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

std::ifstream file(name.c_str());
Run Code Online (Sandbox Code Playgroud)

在c ++ 11中添加了对std::string参数的支持.

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