简体   繁体   English

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

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

I am attempting to create a program to take in file input the user specifies, then count the frequency of each letter, then produces individual text files for each paragraph that it counts. 我正在尝试创建一个程序来接受用户指定的文件输入,然后计算每个字母的频率,然后为它计算的每个段落生成单独的文本文件。

This program compiles perfectly on Microsoft Visual Studio Enterprise 2017. However, when I attempt to run it on my schools computer (running GCC 4.8.5), the program fails and throws errors such as: 该程序可以在Microsoft Visual Studio Enterprise 2017上完美编译。但是,当我尝试在学校计算机上运行它(运行GCC 4.8.5)时,该程序失败并抛出诸如以下的错误:

"no matching function for call to 'std::basic_ifstreamr>::open(std::string&, const openmode&)'" “没有匹配的函数可以调用'std :: basic_ifstreamr> :: open(std :: string&,const openmode&)'”

Any thoughts or suggestions? 有什么想法或建议吗?

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

}

makefile 生成文件

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

I cannot reproduce this with my G++ 7.2.0 compiler, but I think it's possible that your compiler didn't find the cast from std::string to const char* , which is the type of the first parameter of std::basic_fstream::open() . 我无法使用我的G ++ 7.2.0编译器来重现此问题,但是我认为您的编译器可能找不到从std::stringconst char* ,这是std::basic_fstream::open()的第一个参数的类型std::basic_fstream::open() So using c_str() you can yield one for use: 因此,使用c_str()可以产生一个供使用:

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

暂无
暂无

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

相关问题 没有用于调用 &#39;std::basic_ifstream 的匹配函数<char> ::basic_ifstream(QString&amp;)&#39; - no matching function for call to ‘std::basic_ifstream<char>::basic_ifstream(QString&)’ 没有匹配的 function 调用 'std::basic_ifstream<char> ::basic_ifstream(std::__cxx11::string&amp;)' ifstream myfile(filename);</char> - no matching function for call to ‘std::basic_ifstream<char>::basic_ifstream(std::__cxx11::string&)’ ifstream myfile(filename); 错误:没有匹配的函数调用&#39;std::basic_ifstream<char> ::basic_ifstream(std::__cxx11::string&amp;)&#39; - error: no matching function for call to 'std::basic_ifstream<char>::basic_ifstream(std::__cxx11::string&)' 错误:无法转换 'std::ifstream {aka std::basic_ifstream<char> }' to 'char**' for getline() function</char> - error: cannot convert ‘std::ifstream {aka std::basic_ifstream<char>}’ to ‘char**’ for getline() function 在std :: basic_ifstream上编译C ++错误 - C++ compile error on std::basic_ifstream 无法转换 'std::ifstream' {aka 'std::basic_ifstream<char> '} 到 'bool' 作为回报</char> - Cannot convert 'std::ifstream' {aka 'std::basic_ifstream<char>'} to 'bool' in return 是否可以将std :: basic_ifstream和std :: basic_ofstream与自定义缓冲区一起使用? - Is it possible to use std::basic_ifstream and std::basic_ofstream with a custom buffer? 为什么`std::basic_ifstream<char16_t> ` 在 C++11 中不起作用? - Why does `std::basic_ifstream<char16_t>` not work in c++11? 错误:&#39;operator&lt;&lt;&#39; 不匹配(操作数类型是 &#39;std::ifstream&#39; {aka &#39;std::basic_ifstream<char> &#39;} 和 &#39;CHAR [39]&#39; {aka &#39;char [39]&#39;})| - error: no match for 'operator<<' (operand types are 'std::ifstream' {aka 'std::basic_ifstream<char>'} and 'CHAR [39]' {aka 'char [39]'})| 在C ++ / CLI中使用std :: basic_ifstream :: get()时出现AccessViolationException,为什么? - AccessViolationException when using std::basic_ifstream::get() in C++/CLI, why?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM