1
votes

I am working on a program and am getting some strange compiler errors when I attempt to compile with Cygwin. The program compiles and runs just fine with Visual Studio C++ Express, so it seems to have something to do with Cygwin. All of the errors seem to be the same type. Here is an example of code:

int count_records(void)
{
EMPL_TYPE empl_rec;
fstream empl_infile;
empl_infile.open(filepath, ios::in|ios::binary);
int count = 0;
empl_infile.read((char *) &empl_rec, sizeof(empl_rec));
while (!empl_infile.eof())
{
    count++;

    empl_infile.read((char *) &empl_rec, sizeof(empl_rec));
}
empl_infile.close();
cout << "Records Counted: " << count << endl;
return count;
}

And here is the error related to that section:

Assignment2.cpp: In function int count_records()': Assignment2.cpp:34: error: no matching function for call tostd::basic_fstream >::open(const std::string&, std::_Ios_Openmode)' /usr/lib/gcc/i686-pc-cygwin/3.4.4/include/c++/fstream:819: note: candidates are: void std::basic_fstream<_CharT, _Traits>::open(const char*, std::_Ios_Openmode) [with _CharT = char, _Traits = std::char_traits]

Again, I do not get this error when compiling with Visual Studio, only with Cygwin. If anyone has any ideas, it would be appreciated. Thank you.

2

2 Answers

3
votes

Amazingly, the open() method for file streams took only C-style strings until the C++11 standard came out. Either replace your open statement with empl_infile.open(filepath.c_str(), ios::in|ios::binary); (note the .c_str() call on filepath) or add -std=c++11 to your compile line in Cygwin.

2
votes

It could be that you have C++11 support on VS, and not on Cygwin. The fstream::open method takes a const char* as first parameter in C++03. C++11 provides an overload taking an const std::string&.

You can fix it like this

mpl_infile.open(filepath.c_str(), ios::in|ios::binary);