jviotti
jviotti

Reputation: 18939

fstream weird behavior

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

Upvotes: 1

Views: 482

Answers (1)

hmjd
hmjd

Reputation: 122011

The return value of Task::serializeObject() is an fstream, not an fstream&: this is attempting to make a copy of stream and streams are non-copyable. From std::ios_base::ios_base:

The copy constuctor is deleted: streams are not copyable

Change to:

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

Note I have dropped the const as I am unsure why this would be required.

Also, this:

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

will not do what you think. A std::string will contain a pointer to the actual data, and the actual data will not get written to stream using this. Use std::string::c_str(), possibly with std::string::length() so the length of the string is known when re-reading.

Upvotes: 3

Related Questions