没有匹配的函数 - ifstream open() [英] No matching function - ifstream open()

查看:661
本文介绍了没有匹配的函数 - ifstream open()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是代码的一部分出现错误:

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

好吧,我不知道发生了什么。整个事情在VS中正确编译。但我需要编译这个与开发cpp。

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:

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

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

no matching function for call 'std::basic_ifstream::open(std::string&)
no matching function for call 'std::basic_ofstream::open(std::string&)

在代码的不同部分,例如'numeric_limits不是std'的成员,或'max()没有被声明',虽然他们存在于iostream类中,一切都在VS中工作。

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.

为什么会收到此错误?

推荐答案

更改为:

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());

支持 std :: string 参数添加到c ++ 11中。

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

As loadNumbersFromFile()不修改其参数传递 std :: string const& 避免不必要的复制

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

这篇关于没有匹配的函数 - ifstream open()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆