5

I've the following problem..

I have a file called A.h and a file B.h. each contains some struct like this: (the structs inside the two classes are different)

struct Base
{
    friend class access;

    template <typename Archive>
    void serialize(Archive& ar,const unsigned int version)
    {
        ar & fieldLength;
        ar & fieldMD;
        ar & fieldTime_Stamp;
    }   
   public:
        unsigned int fieldLength;
        unsigned int fieldMD;
        unsigned int fieldTime_Stamp;

       virtual void f(){} //to be polymorphic the struct
};

struct Derived:public Base
{
    ...
}

So i serialize the struct in the classic manner:

....

std::ostringstream archive_stream;

boost::archive::text_oarchive archive(archive_stream);

archive.register_type(static_cast<Derived*>(NULL))

archive <<p;   // where p is a pointer to Base

NOW THE PROBLEM... on the deserialization side, I follow the same (inverse) procedure...if I deserialize singularly the structs in A.h (without include in the project B.h) and the structs in B.h (without include in the project A.h) all works....but If I include in the project both the classes, the deserialization works for one class, but throws the "Stream error exception" in the instruction " archive >> m;" for the other...it seems a conflict in the registration class or something like this... Any ideas?thanks...

4
  • Show your actual deserialization code. Commented May 19, 2011 at 7:03
  • std::istringstream archive_stream(mex); //mex is the serialized data received form the socket boost::archive::text_iarchive archive(archive_stream); archive.register_type(static_cast<Derived*>(NULL)); ... Base* m; archive >> m; Commented May 19, 2011 at 7:07
  • I've observed that I have a conflict on registration of the class through the archive.register_type(), despite I create two (local)text_iarchive in separate processes(I use the fork call)...in fact, if I comment the registration code of the second process, all works...how is it possible? Commented May 19, 2011 at 9:43
  • 2
    If you use fork, you have to do the registration either before the fork (once) or after the fork (twice, one in each process). Commented May 19, 2011 at 10:19

1 Answer 1

2

Have you serialised the base data in the derived class serialize function?

ar & boost::serialization::make_nvp( "base", boost::serialization::base_object< Base >( *this ) );

And I dont know if this would help, but I use

BOOST_CLASS_IMPLEMENTATION(x, boost::serialization::object_serializable);
BOOST_CLASS_TRACKING(x, boost::serialization::track_never)

to register the classes as serialiseable. Hope that helps :3

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.