错误代码:无匹配函数供呼叫

error code: no matching function for call to

本文关键字:呼叫 函数 无匹配 错误代码      更新时间:2023-10-16

我正在尝试读取文件并将其存储在数组中。

这是我用来打开文件的代码的一部分:

int readFile(AccessRecord & file, Files & namesIn)
{    
    std::ifstream fin(file.fileName);
    if (fin.fail())
    return -1;
}

这是我去编译时遇到的新错误:

[meg21allred@LinuxLab08 ~]$ g++ myAssign02.cpp
myAssign02.cpp: In function ‘int readFile(AccessRecord&, Files&)’:
myAssign02.cpp:72:35: error: no matching function for call to
‘std::basic_ifstream<char>::basic_ifstream(std::string&)’
std::ifstream fin(file.fileName);

如果我只使用std::cout << file.fileName;,它似乎可以打印出正确的文件名,但是当我将file.fileName放入ifstream line

时,它不喜欢它。

看起来您正在使用一个尚未支持ifstream构造函数的非常旧的编译器,该 CC_4构造函数在2011年添加到C 标准库中。取了const char*(老式的C风格字符串(,因此请尝试:

std::ifstream fin(file.fileName.c_str());