views:

51

answers:

1

I am having a problem where I am trying to serialize a message of a template class. The template's class message is of type BaseClass, but I want it to serialize the derived versions of the class. As of right now, it is only serializing the BaseClass. How am I supposed to register with boost::serialization the types of the derived classes that I want it to serialize? I have tried several methods such as using the 'BOOST_CLASS_EXPORT' macro. Also I have tried using this example:

ar.register_type(static_cast<bus_stop_corner *>(NULL));

from here: http://www.boost.org/doc/libs/1_36_0/libs/serialization/example/demo_gps.hpp but still no luck.

template <class T>
class Frame{
...
private:
    T message;
};

class BaseType{};

class SubTypeA : public BaseType{};

class SubTypeB : public BaseType{};

int main(){
    std::vector< Frame<BaseType> > myFrames;
    //add a bunch of Frame<SubTypeA> and Frame<SubTypeB> objects to the myFrames vector.

    //serialize the vector.

    return 0;
}

This code is not compilable at all, but was included to give you an idea of the structure of my program.

A: 

Make sure you BOOST_CLASS_EXPORT all of your derived classes.

See my answer to this question for a more in-depth answer: http://stackoverflow.com/questions/3148794/c-boostserialization-setting-a-fixed-class-id-for-a-class/3191111#3191111

There's a working example and all there.

Staffan