I’m trying to serialize some private attributes of my class:
class Task {
public:
enum Status { COMPLETED, PENDIENT };
// BLAH BLAH BLAH
// CLASS GETTERS SETTERS ETC...
const std::fstream serializeObject( std::fstream &stream );
private:
void setID();
static int sCount;
int id;
std::string text;
Status status;
tm timestamp;
};
The serializeObject is defined like this:
const std::fstream Task::serializeObject( std::fstream &stream ) {
stream.write((char *) &id, sizeof(int));
stream.write((char *) &text, sizeof(std::string));
stream.write((char *) &status, sizeof(Status));
stream.write((char *) ×tamp, sizeof(tm));
return stream;
}
GCC returns lot of errors:
In file included from /usr/include/c++/4.4/ios:39,
from /usr/include/c++/4.4/ostream:40,
from /usr/include/c++/4.4/iostream:40,
from task.cpp:1:
/usr/include/c++/4.4/bits/ios_base.h: In copy constructor ‘std::basic_ios<char, std::char_traits<char> >::basic_ios(const std::basic_ios<char, std::char_traits<char> >&)’:
/usr/include/c++/4.4/bits/ios_base.h:790: error: ‘std::ios_base::ios_base(const std::ios_base&)’ is private
/usr/include/c++/4.4/iosfwd:47: error: within this context
/usr/include/c++/4.4/iosfwd: In copy constructor ‘std::basic_fstream<char, std::char_traits<char> >::basic_fstream(const std::basic_fstream<char, std::char_traits<char> >&)’:
/usr/include/c++/4.4/iosfwd:87: note: synthesized method ‘std::basic_ios<char, std::char_traits<char> >::basic_ios(const std::basic_ios<char, std::char_traits<char> >&)’ first required here
/usr/include/c++/4.4/streambuf: In copy constructor ‘std::basic_filebuf<char, std::char_traits<char> >::basic_filebuf(const std::basic_filebuf<char, std::char_traits<char> >&)’:
/usr/include/c++/4.4/streambuf:770: error: ‘std::basic_streambuf<_CharT, _Traits>::basic_streambuf(const std::basic_streambuf<_CharT, _Traits>&) [with _CharT = char, _Traits = std::char_traits<char>]’ is private
/usr/include/c++/4.4/iosfwd:78: error: within this context
/usr/include/c++/4.4/iosfwd: In copy constructor ‘std::basic_fstream<char, std::char_traits<char> >::basic_fstream(const std::basic_fstream<char, std::char_traits<char> >&)’:
/usr/include/c++/4.4/iosfwd:87: note: synthesized method ‘std::basic_filebuf<char, std::char_traits<char> >::basic_filebuf(const std::basic_filebuf<char, std::char_traits<char> >&)’ first required here
task.cpp: In member function ‘std::fstream Task::serializeObject(std::fstream&)’:
task.cpp:104: note: synthesized method ‘std::basic_fstream<char, std::char_traits<char> >::basic_fstream(const std::basic_fstream<char, std::char_traits<char> >&)’ first required here
What am I doing wrong? Im following this example. Copied the store function and adapted a bit.
The return value of
Task::serializeObject()is anfstream, not anfstream&: this is attempting to make a copy ofstreamand streams are non-copyable. Fromstd::ios_base::ios_base:Change to:
Note I have dropped the
constas I am unsure why this would be required.Also, this:
will not do what you think. A
std::stringwill contain a pointer to the actual data, and the actual data will not get written tostreamusing this. Usestd::string::c_str(), possibly withstd::string::length()so the length of the string is known when re-reading.