mercredi 20 juillet 2016

no matching function call std::basic_ofstream

I'm trying to accomplish binary i/o here and I keep getting this error which is...

no matching function for call to 'std::basic_ofstream<char>::write(int*,unsigned int)'

and

no matching function for call to 'std::basic_ofstream<char>::write(double*,unsigned int)'

Here is my code...

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main()
{
    int x = 123;
    double fx = 12.34;
    ofstream myfile("filename.dat", ios::out | ios::binary);
    myfile.write(&x, sizeof(x));
    myfile.write(&fx, sizeof(fx));
    myfile.close();
    return 0;
}

I've found multiple other posts with the same situation but their fix was usually typing in...

ofstream myfile(filename.c_str(), ios::out | ios::binary);

The compiler still screams at me as I tried it multiple times. I'm using QT creator as my IDE which I've already verified it is using C++11. and also ran the same code in Code::Blocks and get the same error. Its not liking the filename parameter i'm passing to it. I think this has something to do with the first parameter being the const char of the member function of ofstream open()?!

void open(const char *filename, [int mode], [int prot]);

What am I not understanding/missing here?

Aucun commentaire:

Enregistrer un commentaire