fstream 没有匹配的调用函数错误

No matching call function error for fstream

我正在尝试制作一个程序,该程序将从一个文件读取并根据输入整数输出到另一个文件,该值与读入的值不同。我在打开文件时遇到错误。没有匹配的调用函数

#include <fstream>
#include <iostream>
#include <string>
#include <iomanip>
#include <stdio.h>
using namespace std;

int main()
{

char option, m, c;
int amount, n, k;
string fname, dname, ename;

ofstream nstream;
ifstream istream;


cout << "Do you want to encrypt of decrypt? Enter D or E.";
cin >> option;
cout << endl;

while (option != 'D' && option != 'E')
{
    cout << "That is not acceptable" << endl;
    cout << "Do you want to encrypt of decrypt? Enter D or E.";
    cin >> option;
    cout << endl;
}


if (option == 'E')
{
    cout << "Enter the name of the file that you want to encrypt: ";
    cin >> fname;
    istream.open(fname);
    cout<<endl;
    cout << "Enter the name of the file that you want to output: ";
    cin >> ename;
    nstream.open(ename);
    cout<<endl;
}

if (option == 'D')
{
    cout << "Enter the name of the file that you want to decrypt: ";
    cin >> dname;
    istream.open(dname);
    cout<<endl;
    cout << "Enter the name of the file that you want to output: ";
    cin >> ename;
    nstream.open(ename);
    cout<<endl;

}
cout << "Enter the number of digits that you want to de/encrypt(integers): ";
cin >> k;

istream >> m;
while (m >= 0);
{
    if (m > 65 && m < 90);
    {
        c = m + k;
        while (c > 90)
        {
            c = c - 26;
        }

    }
    if (m > 97 && m < 122);
    {(c = m + k);
    while (c > 122)
    {
        c = c - 26;
    }
    }

     if(m >= 0)
    {
        c = m + k;
    }

}

nstream << c;
return 0;

}

错误信息

In function ‘int main()’:
homework4.cpp:39:21: error: no matching function for call to ‘std::basic_ifstream<char>::open(std::string&)’
  istream.open(fname);
                    ^
homework4.cpp:39:21: note: candidate is:
In file included from homework4.cpp:4:0:
/usr/include/c++/4.9/fstream:541:7: note: void std::basic_ifstream<_CharT, _Traits>::open(const char*, std::ios_base::openmode) [with _CharT = char; _Traits = std::char_traits<char>; std::ios_base::openmode = std::_Ios_Openmode]
      open(const char* __s, ios_base::openmode __mode = ios_base::in)
      ^
/usr/include/c++/4.9/fstream:541:7: note:   no known conversion for argument 1 from ‘std::string {aka std::basic_string<char>}’ to ‘const char*’
homework4.cpp:43:21: error: no matching function for call to ‘std::basic_ofstream<char>::open(std::string&)’
  nstream.open(ename);
                    ^
homework4.cpp:43:21: note: candidate is:
In file included from homework4.cpp:4:0:
/usr/include/c++/4.9/fstream:716:7: note: void std::basic_ofstream<_CharT, _Traits>::open(const char*, std::ios_base::openmode) [with _CharT = char; _Traits = std::char_traits<char>; std::ios_base::openmode = std::_Ios_Openmode]
      open(const char* __s,
      ^
/usr/include/c++/4.9/fstream:716:7: note:   no known conversion for argument 1 from ‘std::string {aka std::basic_string<char>}’ to ‘const char*’
homework4.cpp:51:21: error: no matching function for call to ‘std::basic_ifstream<char>::open(std::string&)’
  istream.open(dname);
                    ^
homework4.cpp:51:21: note: candidate is:
In file included from homework4.cpp:4:0:
/usr/include/c++/4.9/fstream:541:7: note: void std::basic_ifstream<_CharT, _Traits>::open(const char*, std::ios_base::openmode) [with _CharT = char; _Traits = std::char_traits<char>; std::ios_base::openmode = std::_Ios_Openmode]
      open(const char* __s, ios_base::openmode __mode = ios_base::in)
      ^
/usr/include/c++/4.9/fstream:541:7: note:   no known conversion for argument 1 from ‘std::string {aka std::basic_string<char>}’ to ‘const char*’
homework4.cpp:55:21: error: no matching function for call to ‘std::basic_ofstream<char>::open(std::string&)’
  nstream.open(ename);
                    ^
homework4.cpp:55:21: note: candidate is:

In file included from homework4.cpp:4:0:
/usr/include/c++/4.9/fstream:716:7: note: void std::basic_ofstream<_CharT, _Traits>::open(const char*, std::ios_base::openmode) [with _CharT = char; _Traits = std::char_traits<char>; std::ios_base::openmode = std::_Ios_Openmode]
      open(const char* __s,
^ /usr/include/c++/4.9/fstream:716:7: note:   no known conversion for argument 1 from ‘std::string {aka std::basic_string<char>}’ to ‘const char*’

在您的代码中,fnamednameename 默认是 std::string 类型的变量。但是,fstream::open 需要传递给它的 const char * 值作为文件名。

函数的使用方法如下:

void open (const char* filename,
            ios_base::openmode mode = ios_base::in | ios_base::out); 

Obtained from C++ Reference: std::fstream::open

为此,将 .c_str() 函数添加到文件名变量的末尾:

  • istream.open(fname.c_str());
  • nstream.open(ename.c_str());
  • istream.open(dname.c_str());

这将消除该错误。