Boost serialization polymorphic register(export) not working across files

前端 未结 1 886
无人及你
无人及你 2021-02-04 21:18

I am using boost::serialization in my project. The project is large, and serializes my objects in several places. According to the documentation here, I should exp

相关标签:
1条回答
  • 2021-02-04 22:05

    Before calling BOOST_CLASS_EXPORT_* you should include the archives which you want to use. The makro then adds specific serialize-functions for the headers.

    This means you should change your code in hier.cpp to the following:

    #include <boost/serialization/export.hpp>
    #include <boost/archive/text_iarchive.hpp>
    #include <boost/archive/text_oarchive.hpp>
    #include "hier.h"
    
    BOOST_CLASS_EXPORT_IMPLEMENT(D1);
    BOOST_CLASS_EXPORT_IMPLEMENT(D2);
    

    The code in hier.h changes accordingly:

    #include <boost/serialization/export.hpp>
    #include <boost/archive/text_iarchive.hpp>
    #include <boost/archive/text_oarchive.hpp>
    
    BOOST_CLASS_EXPORT_KEY(B);
    BOOST_CLASS_EXPORT_KEY(D1);
    BOOST_CLASS_EXPORT_KEY(D2);
    

    Sources:
    Boost Serializsation Documentation

    PS:
    I do not know if this is solving your problem, but I think it could be causing some trouble. I think its worth a try.

    0 讨论(0)
提交回复
热议问题