没有用于调用std :: basic_ifstream

时间:2018-01-24 03:24:24

标签: c++ c++11

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

此程序在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

1 个答案:

答案 0 :(得分:0)

我无法使用我的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);