fstream没有匹配的呼叫功能错误

时间:2017-03-29 22:58:38

标签: c++ syntax-error 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*’

1 个答案:

答案 0 :(得分:0)

默认情况下,在您的代码中,fnamednameenamestd::string类型的变量。但是,fstream::open要求为文件名传递const char *值。

以下是该功能的使用方法:

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

C++ Reference: std::fstream::open

获取

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

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

这将删除该错误。