我正在尝试序列化我班级的一些私有属性:
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;
};
serializeObject 的定义如下:
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 返回很多错误:
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
我究竟做错了什么?我按照这个例子。复制了商店功能并稍作调整。