<分区>
我希望我的程序使用下面的“readFile”函数读取文件。我试图找出如何使用 istream& 参数调用函数。该函数的目标是通过接收文件名作为参数来读取文件。
#include <iostream>
#include <sstream>
#include <fstream>
#include <string>
using namespace std;
bool readFile(std::istream& fileName); //error 1 this line
int main(void)
{
string fileName;
cout << "Enter the file name: ";
cin >> fileName;
readFile(fileName); //error 2 this line
}
bool readFile(std::istream& fileName)
{
ifstream file(fileName, ios::in); //error 3 this line
return true;
}
我得到的三个错误:
错误 1:传递 'bool readFile(std::istream&) 的参数 1
错误 2:从类型为“std::string {aka std::basic_string&}”的表达式对类型为“std::istream& {aka std::basic_istream&}”的引用的初始化无效
错误 3:从“std::istream {aka std::basic_istream}”到“const char*”的无效用户定义转换 [-fpermissive]
无论如何我可以修复它吗?该函数的参数确实必须保留为“std::istream& fileName”。
感谢您的帮助。