I need to serialize an object that include an hash_map with another object as key. The object that is used as key is a base class for other objects. I have implemented the serialize() method in the base class and in derived classes, and each derived class inherits the serialization method of the base class. The situation is similar to this:
#include <boost/archive/binary_oarchive.hpp>
#include <boost/archive/binary_iarchive.hpp>
#include <boost/serialization/hash_map.hpp>
#include <boost/serialization/base_object.hpp>
class Item {
protected:
unsigned int _refc;
static unsigned int _total_alloc;
//other code
private:
friend class boost::serialization::access;
template<class Archive>
void serialize(Archive & ar, const unsigned int version)
{
ar & _refc;
ar & _total_alloc;
}
};
class StringItem : public Item {
private:
string _s;
friend class boost::serialization::access;
template<class Archive>
void serialize(Archive & ar, const unsigned int version)
{
ar & boost::serialization::base_object<Item>(*this);
ar & _s;
}
};
This is the class that i need to serialize:
class TokenFinder : public Model {
public:
TokenFinder(void);
virtual ~TokenFinder(void);
virtual void insert_item(Item *item);
private:
/** Map to store tokens together with their number of occurrences.
*/
__gnu_cxx::hash_map<Item *, unsigned long> _elements;
unsigned long _element_count;
friend class boost::serialization::access;
template<class Archive>
void serialize(Archive & ar, const unsigned int version)
{
ar & _elements; //Error when saved
ar & _element_count;
}
};
When I try to serialize a TokenFinder object the error is: terminate called after throwing an instance of 'boost::archive::archive_exception' what(): unregistered class - derived class not registered or exported
Any suggestions? Thanks in advance!