lundi 26 juin 2017

C++ error in cygwin; no matching function for call to 'std::basic_fstream...'

I'm using cygwin on Windows 10. When I try to run a very simple program, one that displays lines from a file to the command prompt, I get this error:

$ c++ -c test.cpp
test.cpp: In function ‘int main()’:
test.cpp:9:31: error: no matching function for call to ‘std::basic_fstream<char>::open(std::string&, const openmode&)’
myfile.open(filename, ios::in);
                             ^
In file included from test.cpp:2:0:
/usr/lib/gcc/x86_64-pc-cygwin/5.4.0/include/c++/fstream:1001:7: note: candidate: void std::basic_fstream<_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/lib/gcc/x86_64-pc-cygwin/5.4.0/include/c++/fstream:1001:7: note:   no known conversion for argument 1 from ‘std::string {aka std::basic_string<char>}’ to ‘const char*’

I thought it was a local install issue but I installed cygwin on a second computer and the same thing happened. I'm at a loss.

Sidenote; when I included the ifstream header, the error changed to "Fatal error: ifstream: No such file or directory.

Here is my code:

#include <iostream>
#include <fstream>

using namespace std;

int main()
{
    string filename="names.txt";
    fstream myfile;
    myfile.open(filename, ios::in);
    string firstname, lastname, id;
    for (int i = 0; i < 1; ++i)
    {
            myfile >> firstname >> lastname >> id;
            cout << firstname << " " << lastname << id <<endl;
    }
    return 0;
}

Any help would be much appreciated! This is my first post here, so if I committed any faux pas please let me know.

Aucun commentaire:

Enregistrer un commentaire