2

I am creating a class for writing file in c++, and i have this code so far,

#include "stdafx.h" #include <iostream> #include <string> #include <stdlib.h> #include <fstream> using namespace std; class FileWriter { private: bool isLittleEndian; ofstream file; public: FileWriter(string fileName) : file("data.bin", ios::out | ios::binary) { int i = 1; char *p = (char *)&i; if(p[0] == 1) isLittleEndian = true; } void writeByte() { } void writeShort() { } void writeInt() { } void writeLong() { } void writeUnsignedByte() { } void writeUnsignedShort() { } void writeUnsignedInt() { } void writeUnsignedLong() { } void writeFloat() { } void writeDouble() { } void writeString() { } void closeFile() { file.close(); } }; int main() { FileWriter writer = FileWriter("C:\\Users\\Owner\\Desktop\\Test.bin"); writer.closeFile(); return 0; } 

but for some reason, it wont let me have an ofstream field, and when i try it says, Error 1 error C2248: 'std::basic_ofstream<_Elem,_Traits>::operator =' : cannot access private member declared in class 'std::basic_ofstream<_Elem,_Traits>' c:\users\owner\documents\visual studio 2012\projects\bytetests\bytetests\bytetests.cpp 25 1 I need this because functions in my class need to manipulate this stream. I don't see why this so hard to do.

4
  • You shouldn't your code, it makes all the answers irrelevant. But there doesn't seem to be anything obviously wrong now, so the error must be elsewhere. Commented Oct 17, 2013 at 21:30
  • well then why wont it work, can you compile it Commented Oct 17, 2013 at 21:32
  • I can compile this simplified version. Commented Oct 17, 2013 at 21:33
  • @Popgalop I updated my answer which should fix your remaining problem. Commented Oct 17, 2013 at 21:34

3 Answers 3

4

You can't copy or assign an std::ofstream, which means you can't do this:

file = openedFile; 

You need to either initialize it correctly, or move-copy-assign.

Initialization (preferred option):

FileWriter(string fileName) : file("data.bin", ios::out | ios::binary) { ... } 

Move-copy assignment:

file = std::move(openedFile); 

Alternatively, you can use the std::ofstream::open method:

file.open("data.bin", ios::out | ios::binary); 
Sign up to request clarification or add additional context in comments.

4 Comments

@Popgalop I have added some explanation.
i still get this error Error 1 error C2248: 'std::basic_ofstream<_Elem,_Traits>::basic_ofstream' : cannot access private member declared in class 'std::basic_ofstream<_Elem,_Traits>' c:\users\owner\documents\visual studio 2012\projects\bytetests\bytetests\bytetests.cpp 87 1 ByteTests
@Popgalop I had a typo in my answer, See edit. And simplified example.
i updates my code in my question with your suggested edits but i still get an error
2
file = openedFile; 

There is only one operator= defined for basic_ofstream, the move-assignment operator. The copy-assignment operator is implicitly deleted, and so this code will not work.

You need to use the member-initializer list to initialize file with the path:

FileWriter(std::string fileName) : file(fileName, ios::out | ios::binary) { ... } 

4 Comments

I still just get this error 2 IntelliSense: "std::basic_ofstream<_Elem, _Traits>::basic_ofstream(const std::basic_ofstream<_Elem, _Traits>::_Myt &_Right) [with _Elem=char, _Traits=std::char_traits<char>]" (declared at line 1034 of "C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\include\fstream") is inaccessible c:\Users\Owner\Documents\Visual Studio 2012\Projects\ByteTests\ByteTests\ByteTests.cpp 13 11 ByteTests
@Popgalop Remove the original ofstream openFile(...) and file = openedFile
Error 1 error C2248: 'std::basic_ofstream<_Elem,_Traits>::basic_ofstream' : cannot access private member declared in class 'std::basic_ofstream<_Elem,_Traits>' c:\users\owner\documents\visual studio 2012\projects\bytetests\bytetests\bytetests.cpp 84 1 ByteTests
@Popgalop I don't think it has anything to do with the code you just showed us...
2

You can just open the existing object:

file.open("data.bin", ios::out | ios::binary); 

And you need to avoid calling (or at least requiring) the copy-ctor of your class. Change

FileWriter writer = FileWriter("C:\\Users\\Owner\\Desktop\\Test.bin"); 

to:

FileWriter writer("C:\\Users\\Owner\\Desktop\\Test.bin"); 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.