Error: No matching function to call

Basically I have my code lined out to what I thought would work, but I guess I dont understand fstream completely. I thought I could fstream and inFile to this txt, but I cant get it to compile.


Error is:

encryption.cpp: In function ‘int main()’:
encryption.cpp:18: error: no matching function for call to ‘std::basic_fstream<char, std::char_traits<char> >::basic_fstream(std::string&, const std::ios_base::openmode&)’
/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/fstream:795: note: candidates are: std::basic_fstream<_CharT, _Traits>::basic_fstream(const char*, std::ios_base::openmode) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/fstream:782: note: std::basic_fstream<_CharT, _Traits>::basic_fstream() [with _CharT = char, _Traits = std::char_traits<char>]

Any tips would be much appreciated



Edit: removed the part where I tried to initiliaze a fileName, it wasnt needed and caused an error
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <iostream>
#include <string>
#include <fstream>
using namespace std;

int main()
{
  string  fileName, encrypt;
  char ch;


  // Ask the user to enter the name of the unencrypted file
  cout << "Enter the name of the file to encrypt: ";
  cin  >> fileName;
  cout << "Enter name for encrypted file: ";
  cin  >> encrypt;

  fstream inFile(fileName, ios::in);
  if (!inFile)
    {
      cout << "Error opening file \"" << fileName << "\".\n";
      return 0;
    }

  fstream outFile(encrypt, ios::out);

  while (!inFile.fail())
    {
      inFile.get(ch);
      ch += 10;
      outFile << ch;
    }

  inFile.close();
  outFile.close();
  return 0;
}


Last edited on
In my system it works fine, for example
from abcdef to klmnopz
Try to write ios_base instead of ios
but im not sure
It compiled when I turned on C++11 support, which allows you to use a string in the open file statements.

Either:
- set a compiler option to allow support for the C++ standard (mine is -std=c++11)
or
- change the open file lines to
fstream inFile(fileName.c_str(), ios::in);
and
fstream outFile(encrypt.c_str(), ios::out);
which will convert the strings to the Cstrings needed by the original standard.

See:
http://www.cplusplus.com/reference/fstream/fstream/fstream/
Last edited on
- change the open file lines to
fstream inFile(fileName.c_str(), ios::in);
and
fstream outFile(encrypt.c_str(), ios::out);
which will convert the strings to the Cstrings needed by the original standard.


This fixed it, thanks. Basically my teacher is oldschool, and our compiler didn't understand the format I learned off the forums and internet, and needed the older format. Thanks for all the help. You guys are the best!
Topic archived. No new replies allowed.