Boost logo

Boost Users :

From: Avery Fay (btmore_at_[hidden])
Date: 2007-06-27 12:12:47


Hello,

I'm having an issue with serializing derived classes from a base class
pointer. I think I've narrowed it down to the simplest possible code. I get
an "unregistered class" exception at runtime even though I've registered the
derived class with BOOST_CLASS_EXPORT. Here's the code:

----------------------------------------------

main.cpp:

#include <boost/archive/text_oarchive.hpp>
#include <boost/archive/text_iarchive.hpp>
#include <sstream>

#include "A.hpp"
#include "X.hpp"

int main()
{
    std::stringstream ss;

    const A * const test = new X;

    {
        boost::archive::text_oarchive oa(ss);
        oa << test;
    }

    A *test2;

    {
        boost::archive::text_iarchive ia(ss);
        ia >> test2;
    }

    return 0;
}

-----------------------------------------------

A.hpp:

#pragma once

#include <boost/serialization/access.hpp>

class A
{
private:
    friend class boost::serialization::access;
    template<class Archive>
    void serialize(Archive & ar, const unsigned int)
    {
        ar & test;
    }
    int test;
public:
    A() : test(0) {}
    virtual ~A() {}
};

-----------------------------------------------

X.hpp:

#pragma once

#include <boost/serialization/base_object.hpp>
#include "A.hpp"

class X : public A
{
    friend class boost::serialization::access;
    template<class Archive>
    void serialize(Archive & ar, const unsigned int)
    {
        ar & boost::serialization::base_object<A>(*this);
    }
};

------------------------------------------------

X.cpp:

#include "X.hpp"

#include <boost/serialization/export.hpp>

BOOST_CLASS_EXPORT(X)

-----------------------------------------------

As you can see, I don't have BOOST_CLASS_EXPORT in the X.hpp header. This is
the way I originally had it but then I got multiple definition link errors
when more than one class derived from A. The suggestion in the serialization
docs was to move the macro to a .cpp file.

Full output when I run this:

terminate called after throwing an instance of
'boost::archive::archive_exception'
  what(): unregistered class
Aborted (core dumped)

Any help would be appreciated greatly,
Avery



Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net