1

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 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

Comments

2

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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.