I’m programming my first big “class” in a c++ program (it deals with I/O stream) and I think I understood the concepts of object, methods and attributes.
Though I guess I still don’t get all the rights of the encapsulation notion,
because I want my class called File to have
- a name (the path of the file),
- a reading stream, and
- a writing stream
as attributes,
and also its first method to actually get the “writing stream” attribute of the File object…
#include <string>
#include <fstream>
class File {
public:
File(const char path[]) : m_filePath(path) {}; // Constructor
File(std::string path) : m_filePath(path) {}; // Constructor overloaded
~File(); // Destructor
static std::ofstream getOfstream(){ // will get the private parameter std::ofStream of the object File
return m_fileOStream;
};
private:
std::string m_filePath; // const char *m_filePath[]
std::ofstream m_fileOStream;
std::ifstream m_fileIStream;
};
But I get the error:
Error 4 error C2248: ‘std::basic_ios<_Elem,_Traits>::basic_ios’ :
cannot access private member declared in class
‘std::basic_ios<_Elem,_Traits>’ c:\program files (x86)\microsoft
visual studio 10.0\vc\include\fstream 1116
reporting me to the following part of fstream.cc :
private:
_Myfb _Filebuffer; // the file buffer
};
Could you then help me to fix this and be able to use a stream as parameter of my class please? I have tried to return a reference instead of the stream itself, but I’m gonna need some help with that as well (doesn’t work either…).
Thanks in advance
change
to