【问题标题】:No matching function for call to std::basic_ifstream没有匹配函数调用 std::basic_ifstream
【发布时间】:2018-01-24 03:24:23
【问题描述】:

我正在尝试创建一个程序来接收用户指定的文件输入,然后计算每个字母的频率,然后为它计算的每个段落生成单独的文本文件。

该程序在 Microsoft Visual Studio Enterprise 2017 上完美编译。但是,当我尝试在我的学校计算机上运行它(运行 GCC 4.8.5)时,该程序失败并抛出如下错误:

"没有匹配函数调用'std::basic_ifstreamr>::open(std::string&, const openmode&)'"

有什么想法或建议吗?

字母计数.​​hpp

#ifndef LETTERCOUNT_HPP
#define LETTERCOUNT_HPP

#include <iostream>
#include <fstream>
#include <string>

using std::cout;
using std::cin;
using std::endl;
using std::ifstream;
using std::ofstream;

class letterCount

{
private:
public:

    //Letter counting and output functions
    void countTheLetters(ifstream &, int*);
    void outputTheLetters(ofstream &, int*);
};
#endif

字母计数.​​cpp

#include "letterCount.hpp"
#include <iostream>
#include <fstream>
#include <string>

using std::cout;
using std::endl;
using std::cin;
using std::string;
using std::ios;
using std::ofstream;
using std::ifstream;

//Function for count_letters

void letterCount::countTheLetters(ifstream &finput, int* count)

{
    //Declaring a string variable
    string stringOfWords = "", line;
    int k = 0;
    char character;
    //While loop through the text
    while (getline(finput, line))
    {
       if (line.empty()) {
          break;
       }
       else {
          //Lines are concatinated
          stringOfWords += line + ' ';
       }
    }
    //White space removal
    stringOfWords.erase(stringOfWords.length() - 1);
    //Value is zero
    for (k = 0; k<26; k++) {
       count[k] = 0;
    }
    //Count each letter individually
    for (k = 0; k<stringOfWords.length(); k++) {
       //One letter at a time
       character = tolower(stringOfWords[k]);
       //Character validation
       if ((int)character >= 97 && (int)character <= 122){
          //Update the frequency counter
          count[(int)character - 97] += 1;

       }

    }

}

//Function for output_letters
void letterCount::outputTheLetters(ofstream &foutput, int* count){
    //Variable placeholder declaration
    int k;
      //For loop to print out the frequency of the letters
    for (k = 0; k<26; k++){
       //Output to file
       foutput << (char)(k + 97) << " - " << count[k] << "\n ";

    }

}

main.cpp

//Standard include files, including letterCount.hpp
#include "letterCount.hpp"
#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>

using std::cout;
using std::endl;
using std::cin;
using std::string;
using std::ios;
using std::ofstream;
using std::ifstream;

int main() {

    ifstream finput;
    ofstream foutput;
    string input, output;

    int count[26] = { 0 }, k = 1;

    //Request user input of the file to be used
    cout << "\n Enter the input file - ";
    cin >> input;

    //Open the file
    finput.open(input, ios::in);

    //While loop through the file
    while (finput.good()) {

       //Countint the letters
       letterCount a;
       a.countTheLetters(finput, count);
       cout << "\n Enter the output file " << k << " - ";
       cin >> output;

       //The number of paragraph is updated
       k = k + 1;

       //Open the receiving file
       foutput.open(output, ios::out);
       a.outputTheLetters(foutput, count);

       //Close the file
       foutput.close();

    }

    //Close the input file
    finput.close();
    system("pause");
    return 0;

}

制作文件

output: main.o letterCount.o
    g++ -std=c++0x main.o letterCount.o -o output

main.o: main.cpp
    g++ -c main.cpp

letterCount: letterCount.cpp letterCount.hpp
    g++ -c letterCount.cpp

clean:
    -rm *.o output

【问题讨论】:

  • 错误出现在哪一行?我无法重现它。
  • ifstream::open with a string input is in C++11.可能你学校的编译器不支持它。尝试传递 char*
  • 开启c++11 stackoverflow.com/questions/16886591/how-do-i-enable-c11-in-gccfinput.open(input.c_str(), ios::in);
  • @iBug 我在第 29 行和第 44 行收到错误。
  • 感谢@RetiredNinja 的洞察力。您的建议奏效了。

标签: c++ c++11


【解决方案1】:

我无法用我的 G++ 7.2.0 编译器重现这一点,但我认为您的编译器可能没有找到从 std::stringconst char* 的转换,这是 std::basic_fstream::open() 的第一个参数的类型.所以使用c_str() 你可以生成一个供使用:

finput.open(input.c_str(), ios::in);
foutput.open(output.c_str(), ios::out);

【讨论】:

    猜你喜欢
    • 2020-07-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-06
    • 2017-05-01
    • 1970-01-01
    相关资源
    最近更新 更多