I'm programming my first big "class" in a c++ program (it deals with I/O stream) and I think I understood all 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...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
#include <string>
#include <fstream>
class File {
public:
File(constchar 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[] doesn't wanna work
static std::ofstream m_fileOStream;
static 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 :
1 2 3
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?
Thanks in advance,
(-_-;) I thought I could try to bypass the problem with this, ne555...
If I return a &reference, shouldn't I make this method const?
And will I then be able to use it like &fileOStream.rdbuf()?
Thank you very much to both of you though, I highly appreciate.
I already took off the static in front of the attributes stream. As I told you, I had tried to bypass the problem by making them static and I just forgot to take off the keyword... Sorry about that.
I'd like to provide access to the stream because it would allow me to present my program under the form of different methods related to my different files (with streams and other stuff as parameter).
This function you wrote me about seems very interesting, though, as I understood it'd allow me to use the object File as I wanted to use its attribute stream.
Though when I try to compile it I get:
Error 51 error LNK2019: unresolved external symbol "public: __thiscall File::~File(void)" (??1File@@QAE@XZ) referenced in function _main C:\Users\GWB\Documents\pipe_GWB9\pipe_GWB9\pipe_GWB9.obj