C++ C++;-将文件处理程序传递到函数以打开时出错

C++ C++;-将文件处理程序传递到函数以打开时出错,c++,file,function,C++,File,Function,我有几个错误,我不明白,甚至不知道如何在互联网上搜索这个。我尝试过“C++文件中的打开文件”以及确切的错误,0,但在最初几页中没有得到很多帮助。这让我头痛了一段时间,甚至我的教练也帮不了我什么忙。尽管这是他想要的方式,不知怎么的。是的,这是家庭作业 错误: \driver.cpp:在函数void openFile(std::ifstream&,std::ofstream&)中: \driver.cpp:63:错误:调用std::basic\u ifstream>::open(std::strin

我有几个错误,我不明白,甚至不知道如何在互联网上搜索这个。我尝试过“C++文件中的打开文件”以及确切的错误,0,但在最初几页中没有得到很多帮助。这让我头痛了一段时间,甚至我的教练也帮不了我什么忙。尽管这是他想要的方式,不知怎么的。是的,这是家庭作业

错误:

\driver.cpp:在函数
void openFile(std::ifstream&,std::ofstream&)中:
\driver.cpp:63:错误:调用
std::basic\u ifstream>::open(std::string&)没有匹配的函数 ../include/c++/3.4.2/fstream:570:注:候选项为:void std::basic_ifstream::open(const char*,std:_Ios\u Openmode)[带_CharT=char,_Traits=std::char\u Traits] J:\Class\u Files\u 2013\u Fall\Advanced C++\Week1\Lab\u 2(字母计数器)\driver.cpp:68:错误:调用'std::basic\u of stream>::open(std::string&)' ../include/c++/3.4.2/fstream:695:注:候选项为:void std::basic_of stream::open(const char*,std:_Ios_Openmode)[带_CharT=char,_Traits=std::char_Traits]

因此,大体上,我有:

    ifstream inFile;                    
//defines the file pointer for the text document
    ofstream outFile;                   
//the file pointer for the output file

    openFile(inFile, outFile);          
//allow the user to specify a file for reading and outputting the stats
然后是函数:

void openFile(ifstream& inF, ofstream& outF){
    // Ask the user what file to READ from
    string readFile, writeFile;
    cout << "Enter the name of the file you want to READ from: ";
    cin >> readFile;
    cout << endl;
        // Open the File
    inF.open(readFile);
    // Ask the user what file to WRITE to
    cout << "Enter the name of the file you want to WRITE to: ";
    cin >> writeFile;
    cout << endl;
    outF.open(writeFile);
}
主要的东西是教练放在那里的,所以我不能改变。换句话说,我必须将文件指针传递到openFile函数中,并向用户询问文件的名称。然而,我从来没有被教过如何做到这一点。如果能给出一个基本的答案,我将不胜感激,因为我已经在做了。

简易修复:

inF.open(readFile.c_str());
outF.open(writeFile.c_str();
这两个函数都希望传入一个常量char*作为第一个参数。您正试图传递一个字符串。

简易修复:

inF.open(readFile.c_str());
outF.open(writeFile.c_str();

这两个函数都希望传入一个常量char*作为第一个参数。您正试图传递一个字符串。

只有C++11支持使用
std::string
s打开fstream


使用C++11兼容性编译应用程序(取决于编译器,对于clang和gcc,它是
-std=C++11
),或者将调用更改为
inF.open(readFile.C_str())

只有C++11支持使用
std::string
s打开fstream

使用C++11兼容性编译应用程序(取决于编译器,对于clang和gcc,它是
-std=C++11
),或者将调用更改为
inF.open(readFile.C_str())

当您看到“无匹配函数”错误时,这意味着编译器搜索了一个函数,但找不到接受您给出的参数的函数。在这种情况下,它找不到接受
std::string&
的函数
open()
的重载

从C++11开始,输入和输出文件流类提供其构造函数的重载,以及将
std::string
作为参数的
open()
成员函数。不幸的是,如果您的编译器不支持C++11,您将不得不使用采用
const char*
的重载。您可以通过调用
c_str()
方法获取指向缓冲区的指针:

inF.open(readFile.c_str());   // calls open(const char*)
outF.open(writeFile.c_str()); // calls open(const char*)
当您看到“无匹配函数”错误时,这意味着编译器搜索了一个函数,但找不到接受您给出的参数的函数。在这种情况下,它找不到接受
std::string&
的函数
open()
的重载

从C++11开始,输入和输出文件流类提供其构造函数的重载,以及将
std::string
作为参数的
open()
成员函数。不幸的是,如果您的编译器不支持C++11,您将不得不使用采用
const char*
的重载。您可以通过调用
c_str()
方法获取指向缓冲区的指针:

inF.open(readFile.c_str());   // calls open(const char*)
outF.open(writeFile.c_str()); // calls open(const char*)

成功了!谢谢你的帮助!我还不能投票给你,但我可以通过评论向你表示感谢。:)+它成功了!谢谢你的帮助!我还不能投票给你,但我可以通过评论向你表示感谢。:)+它成功了!谢谢你的帮助!我还不能投票给你,但我可以通过评论表达我的感谢。谢谢+它成功了!谢谢你的帮助!我还不能投票给你,但我可以通过评论表达我的感谢。谢谢+1感谢您进一步详细描述问题。我现在明白发生了什么。我一直在使用记事本++插件来编译和运行我的文件,那可能不是C++11。我想我现在必须使用const方法。谢谢+1感谢您进一步详细描述问题。我现在明白发生了什么。我一直在使用记事本++插件来编译和运行我的文件,那可能不是C++11。我想我现在必须使用const方法。谢谢+1.
inF.open(readFile.c_str());   // calls open(const char*)
outF.open(writeFile.c_str()); // calls open(const char*)