【问题标题】:C++ - Error passing file handlers into function for openingC++ - 将文件处理程序传递给打开函数时出错
【发布时间】:2014-06-05 01:43:53
【问题描述】:

我有几个我不理解的错误,甚至不知道如何在互联网上进行搜索。我已经尝试过“在 C++ 函数中打开文件”以及确切的错误 0,但在前几页中没有得到太多帮助。这让我头疼了一段时间,甚至我的导师也帮不上什么忙。虽然这是他想要的方式,但不知何故。是的,这是一个家庭作业。

错误:

\driver.cpp: 在函数void openFile(std::ifstream&, std::ofstream&)': \driver.cpp:63: error: no matching function for call tostd::basic_ifstream >::open(std::string&)' ../include/c++/3.4.2/fstream:570:注意:候选人是: void std::basic_ifstream<_chart _traits>::open(const char*, std::_Ios_Openmode) [with _CharT = char, _Traits = std::char_traits] J:\Class_Files_2013_Fall\Advanced C++\Week1\Lab_2(Letter_Counter)\driver.cpp:68: 错误:没有匹配函数调用`std::basic_ofstream >::open(std::string&)' ../include/c++/3.4.2/fstream:695:注意:候选人是: void std::basic_ofstream<_chart _traits>::open(const char*, std::_Ios_Openmode) [with _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);
}

我也实现了:

#include <fstream>

还有:

using namespace std;

主要的东西是教练放在那里的,所以我无法更改。换句话说,我必须将文件指针传递给 openFile 函数并询问用户文件的名称。然而,我从来没有被教过如何做到这一点。一个基本的答案将不胜感激,脱离我已经在做的事情。

【问题讨论】:

    标签: c++ file function


    【解决方案1】:

    只有 C++11 支持使用std::strings 打开 fstream。

    要么编译你的应用,使其兼容 C++11(取决于编译器,对于 clang 和 gcc,它是 -std=c++11),或者将你的调用更改为 inF.open(readFile.c_str());

    【讨论】:

    • 成功了!谢谢您的帮助!我还不能投票给你,但我可以通过评论表示感谢。谢谢! +1
    【解决方案2】:

    简单修复:

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

    这两个函数都希望你传入一个 const char* 作为第一个参数。您正在尝试向其传递一个字符串。

    【讨论】:

    • 成功了!谢谢您的帮助!我还不能投票给你,但我可以通过评论向你表示感谢。 :) +1
    【解决方案3】:
    no matching function for call to std::basic_ifstream::open(std::string&)
    

    当您看到“无匹配函数”错误时,这意味着编译器已搜索但找不到采用您提供的参数的函数。在这种情况下,它找不到函数 open() 的重载,它采用 std::string&amp;

    从 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*)
    

    【讨论】:

    • 感谢您进一步详细描述问题。我现在明白发生了什么。我一直在使用 notepad++ 插件来编译和运行我的文件,这可能不是 C++11。我想我现在必须使用 const 方法。谢谢! +1
    猜你喜欢
    • 1970-01-01
    • 2016-06-23
    • 2023-03-18
    • 2012-08-30
    • 1970-01-01
    • 2012-02-19
    • 1970-01-01
    相关资源
    最近更新 更多