繁体   English   中英

没有匹配的函数调用std :: basic_ifstream

[英]No matching function for call to std::basic_ifstream

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

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

“没有匹配的函数可以调用'std :: basic_ifstreamr> :: open(std :: string&,const openmode&)'”

有什么想法或建议吗?

letterCount.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

letterCount.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

我无法使用我的G ++ 7.2.0编译器来重现此问题,但是我认为您的编译器可能找不到从std::stringconst char* ,这是std::basic_fstream::open()的第一个参数的类型std::basic_fstream::open() 因此,使用c_str()可以产生一个供使用:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM