为什么我会得到“无匹配功能"?我在std :: string中打开文件名的fstream时出错? [英] Why do I get a "no matching function" error when I open an fstream with the file name in a std::string?

查看:117
本文介绍了为什么我会得到“无匹配功能"?我在std :: string中打开文件名的fstream时出错?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个程序,该程序从文件中读取字符串列表,并检查第二个文件中缺少哪些字符串,并将其打印到屏幕上.但是,当我尝试编译时,我当前遇到错误.以下是我尝试编译以及代码时遇到的错误.谢谢您的帮助

I am trying to write a program that reads in a list of strings from a file and checks what strings are missing from a second file and prints them to the screen. However, I am currently getting an error when I try to compile. Below is the error I am getting when I try to compile as well as the code. Thank you for the help

这是代码:

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

using namespace std;

ifstream IN_FILE;
ofstream OUT_FILE;

int main()  { 
    int k = 0;
    int m = 0;
    int n = 0;
    string a[5000];
    string b[5000];
    string filename;
    bool good;

    //open file and check if valid entry
    cout << "\n\nWhat is the name of the first file (ex: filename.txt)\n" << endl << "FILENAME: ";
    getline(cin, filename);
    IN_FILE.open(filename);
    while(!IN_FILE) {
        cout << "Sorry the file you entered could not be opened\n";
        cout << "\n\nWhat is the name of the first file (ex: filename.txt)\n" << endl << "FILENAME: ";
        getline(cin, filename);
        IN_FILE.open(filename);
    }

    //Read every line from file
    while(!IN_FILE.eof()) {
        getline(IN_FILE, a[k]);
        k++;
    }
    n = k;
    k = 0;
    IN_FILE.close();

    //open file and check if valid entry
    cout << "\n\nWhat is the name of the first file (ex: filename.txt)\n" << endl << "FILENAME: ";
    getline(cin, filename);
    IN_FILE.open(filename);
    while(!IN_FILE) {
        cout << "Sorry the file you entered could not be opened\n";
        cout << "\n\nWhat is the name of the first file (ex: filename.txt)\n" << endl << "FILENAME: ";
        getline(cin, filename);
        IN_FILE.open(filename);
    }

    //Read every line from file
    while(!IN_FILE.eof()) {
        getline(IN_FILE, b[k]);
        k++;
    }
    m = k;
    k = 0;
    
    //Compare the arrays and print all elements is array a that are not in array b
    for (int i = 0; i < n; i++)  { 
        int j;
        for (j = 0; j < m; j++) 
            if (a[i] == b[j]) 
                break; 
  
        if (j == m) 
            cout << a[i] << endl; 
    } 
    
    return 0; 
}

这是错误:

checkTester.cpp:25:26: error: no matching function for call to 'std::basic_ifstream<char>::open(std::__cxx11::string&)'
     IN_FILE.open(filename);

推荐答案

此结构适用于标准C ++ 11和更高版本的编译器:

This construct works for standard C++ 11 and newer compilers:

std::string filename;
//...
IN_FILE.open(filename);

这基本上是您现在拥有的代码.但是,以上内容不能与标准C ++ 11编译器(C ++ 98、03)一起使用.

This is basically the code you have now. However, the above will not work with pre-standard C++ 11 compilers (C++ 98, 03).

如果使用标准C ++ 11编译器进行编译,则上面的代码应为:

If you are compiling using a pre-standard C++ 11 compiler, then the code above should be:

std::string filename;
//...
IN_FILE.open(filename.c_str()); // Note the null-terminated const char *

基本上, open std :: string 版本在C ++ 11之前不存在.在C ++ 11之前,您必须使用C样式指定名称,空终止的字符串.

Basically, the std::string version of open did not exist before C++ 11. Before C++ 11, you had to specify the name using a C-style, null-terminated string.

因此,您看到的错误是因为您在C ++ 11之前的C ++版本中进行编译.

Thus the error you are seeing is because you are compiling in a version of C++ before C++ 11.

这篇关于为什么我会得到“无匹配功能"?我在std :: string中打开文件名的fstream时出错?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆