Private member declared in other class

Hi,

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(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[] 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,

Sky
That's because streams can't be copied.
You could return a reference, or wrap the methods that you want.

I think I understood all the concepts of object, methods and attributes.
¿why are the streams static?
If you need to access the internal buffer the rdbuf() member returns a pointer to the private member (my implementation is GCC 4.6.1/Mingw)
(-_-;) 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.
Last edited on
ne555, what does
or wrap the methods that you want
mean?
By instance
1
2
3
4
File& File::operator<<(const char *s){
   m_fileOStream << s;
   return *this;
}
The question is ¿why do you want to provide access to the stream?

If I return a &reference, shouldn't I make this method const?
¿Does it modify the object?
Again, the streams are static so they are not attributes of the object but of the class ¿are you sure that you want that?
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

Would you happen to know why?
Last edited on
You didn't provide the body of the destructor.
Or it is in another file, and you forgot to link it.

If the destructor will "do nothing", you could use the default one provided by the compiler (don't declare it)
Topic archived. No new replies allowed.