Domanda

I try to serialize derived pointer class with non-default constructor with the help of boost.

During the compilation I get an error:

Derived.h: In function ‘void boost::serialization::load_construct_data(Archive&, const A::Derived*, unsigned int)’:

in Derived.h: error: no matching function for call to ‘operator new(long unsigned int, const A::Derived*&)

I included <new> to Derived.h, but I have a feeling that I forgot to do something. Here is a rough estimation of the code I have.

I have a base class with virtual functions and non-default constructor(in Base.h)

#include <boost/archive/text_oarchive.hpp>
#include <boost/archive/text_iarchive.hpp>
#include <boost/serialization/export.hpp>
namespace A{

    class Base

    {

    public:

             int getID(){return ID};

             //non default constructor
             Base(param1, param2, param3):ID(param1+param2), BaseFlag(param3) {};
             Base(param1, param3):ID(param1), BaseFlag(param3) {};

             //some virtual functions
             virtual void Foo1();
             virtual void Foo2();
             ...

    private:

             int ID;
             bool BaseFlag;
             ...

             //void serialize function
             friend class boost::serialization::access;
             template<class Archive>
             void serialize(Archive & ar, const unsigned int version)
             {

             }

    };

}
//end of namespace A
//implementation is in another file - exporting key
BOOST_CLASS_EXPORT_KEY(Base)

There is a derived class (in Derived.h)

#include <boost/serialization/base_object.hpp>
#include <boost/archive/text_oarchive.hpp>
#include <boost/archive/text_iarchive.hpp>
#include <boost/serialization/export.hpp>
#include <new>
#include "Base.h"

namespace A{

    //derived class
    class Derived: public Base
    {
    public:
        //non default constructor
        Derived(param3):Base(param3, false);
        ...
   private:
        friend class boost::serialization::access;
        template<class Archive>

        // serialize base class information
        void serialize(Archive & ar, const unsigned int version)
        {
          ar & boost::serialization::base_object<Base>(*this);
        }

        //prototype of save_construct_data for non-default constructor
        template<class Archive> friend 
            void boost::serialization::save_construct_data(Archive & ar, 
                         const Derived * t, const unsigned int file_version);

        //prototype of load_construct_data for non-default constructor
        template<class Archive> friend 
            void boost::serialization::load_construct_data(Archive & ar, 
                         const Derived * t, const unsigned int file_version);
    };
}
//end of namespace A

//export derived class
BOOST_CLASS_EXPORT_KEY(Derived)

//describe save_construct_data
namespace boost {
    namespace serialization {
        template<class Archive>
        inline void save_construct_data(Archive & ar, const A::Derived * t, const unsigned int file_version)
        {
            // save data required to construct instance
            ar << t->ID;

        }

        template<class Archive>
        inline void load_construct_data(Archive & ar, const A::Derived * t, const unsigned int file_version)
        {
            int ID;
            // load data required to construct instance
            ar >> ID;
            ::new(t) A::Derived(ID);

        }


     }
 }

And somewhere in main.cpp I want to save and load derived class. So, the compilation error I mentioned in the beginning prevents me to proceed .

Any hints what I am missing?

È stato utile?

Soluzione

I believe the compiler is complaining because you are trying to construct a Derived in memory tagged as const. In this declaration:

template<class Archive> friend 
    void boost::serialization::load_construct_data(Archive & ar, 
                 const Derived * t, const unsigned int file_version);

I think you want Derived *t, not const Derived *t.

The boost docs also have no const in the function signature:

template<class Archive>
inline void load_construct_data(
    Archive & ar, my_class * t, const unsigned int file_version
){
    // retrieve data from archive required to construct new instance
    int attribute;
    ar >> attribute;
    // invoke inplace constructor to initialize instance of my_class
    ::new(t)my_class(attribute);
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top