关于C ++:无匹配函数-ifstream open()

No matching function - ifstream open()

这是代码的一部分,带有错误:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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 <<"
Error

"
;
        exit(EXIT_FAILURE);
    }

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

而且,我有点不知道发生了什么。 整个东西可以在VS中正确编译。 但是我需要使用dev cpp进行编译。

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

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

在代码的不同部分,我遇到类似" numeric_limits不是std的成员"或" max()尚未声明"之类的错误,尽管它们存在于iostream类中,并且一切都在VS中起作用。

为什么会出现此错误?


改成:

1
file.open(name.c_str());

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

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

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

因为loadNumbersFromFile()不会修改其参数,所以通过std::string const&传递以记录该事实并避免不必要的复制。