No matching function - ifstream open()

我与影子孤独终老i 提交于 2019-11-26 01:45:44

问题


This is the part of the code with an error:

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;
}

And well, I kind of have no idea what is going on. The whole thing compiles properly in VS. However I need to compile this with dev cpp.

I commented out the line throwing errors in the code above. The errors are:

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

In different parts of code I get errors like numeric_limits is not a member of std, or max() has not been declared, although they exist in iostream class and everything works in VS.


Why am I getting this error?


回答1:


Change to:

file.open(name.c_str());

or just use the constructor as there is no reason to separate construction and open:

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

Support for std::string argument was added in c++11.

As loadNumbersFromFile() does not modify its argument pass by std::string const& to document that fact and avoid unnecessary copy.



来源:https://stackoverflow.com/questions/16552753/no-matching-function-ifstream-open

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!