1

我正在尝试序列化我班级的一些私有属性:

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 *) &timestamp, 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 

我究竟做错了什么?我按照这个例子。复制了商店功能并稍作调整。

4

1 回答 1

3

的返回值Task::serializeObject()是一个fstream,而不是一个fstream&:这是试图制作一个副本,stream并且流是不可复制的。来自std::ios_base::ios_base

复制构造函数被删除:流不可复制

改成:

std::fstream& Task::serializeObject( std::fstream &stream )

注意我已经放弃了,const因为我不确定为什么需要这样做。

另外,这个:

stream.write((char *) &text, sizeof(std::string));

不会做你想的。Astd::string将包含一个指向实际数据的指针,并且使用它不会写入实际数据stream。使用std::string::c_str(), 可能std::string::length()在重新阅读时知道字符串的长度。

于 2012-10-07T21:28:38.570 回答