<small id='oH2vY'></small><noframes id='oH2vY'>

      • <bdo id='oH2vY'></bdo><ul id='oH2vY'></ul>
      <legend id='oH2vY'><style id='oH2vY'><dir id='oH2vY'><q id='oH2vY'></q></dir></style></legend>

      <tfoot id='oH2vY'></tfoot>
        <i id='oH2vY'><tr id='oH2vY'><dt id='oH2vY'><q id='oH2vY'><span id='oH2vY'><b id='oH2vY'><form id='oH2vY'><ins id='oH2vY'></ins><ul id='oH2vY'></ul><sub id='oH2vY'></sub></form><legend id='oH2vY'></legend><bdo id='oH2vY'><pre id='oH2vY'><center id='oH2vY'></center></pre></bdo></b><th id='oH2vY'></th></span></q></dt></tr></i><div id='oH2vY'><tfoot id='oH2vY'></tfoot><dl id='oH2vY'><fieldset id='oH2vY'></fieldset></dl></div>

        以自定义对象为键的 hash_map 的 C++ Boost.Serialization 错误

        C++ Boost.Serialization error for hash_map with custom objects as key(以自定义对象为键的 hash_map 的 C++ Boost.Serialization 错误)
          <tbody id='3AMAx'></tbody>
        • <i id='3AMAx'><tr id='3AMAx'><dt id='3AMAx'><q id='3AMAx'><span id='3AMAx'><b id='3AMAx'><form id='3AMAx'><ins id='3AMAx'></ins><ul id='3AMAx'></ul><sub id='3AMAx'></sub></form><legend id='3AMAx'></legend><bdo id='3AMAx'><pre id='3AMAx'><center id='3AMAx'></center></pre></bdo></b><th id='3AMAx'></th></span></q></dt></tr></i><div id='3AMAx'><tfoot id='3AMAx'></tfoot><dl id='3AMAx'><fieldset id='3AMAx'></fieldset></dl></div>
          1. <tfoot id='3AMAx'></tfoot>

            <small id='3AMAx'></small><noframes id='3AMAx'>

              • <bdo id='3AMAx'></bdo><ul id='3AMAx'></ul>

                  <legend id='3AMAx'><style id='3AMAx'><dir id='3AMAx'><q id='3AMAx'></q></dir></style></legend>
                  本文介绍了以自定义对象为键的 hash_map 的 C++ Boost.Serialization 错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我需要序列化一个包含 hash_map 的对象,并将另一个对象作为键.用作键的对象是其他对象的基类.我在基类和派生类中都实现了serialize()方法,每个派生类都继承了基类的序列化方法.情况类似这样:

                  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;
                   }
                  };
                  

                  当我尝试序列化 TokenFinder 对象时,错误是:在抛出 'boost::archive::archive_exception' 实例后调用终止what():未注册的类 - 未注册或导出的派生类

                  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!

                  推荐答案

                  尝试在使用前向 Archive 注册 Item 的子类:

                  Try registering the subclasses of Item with the Archive prior to use:

                  template<class Archive>
                      void serialize(Archive & ar, unsigned)
                      {
                          ar.template register_type<StringItem>(); // THIS
                  
                          ar & boost::serialization::base_object<Model>(*this);    
                          ar & _elements;
                          ar & _element_count;
                      }
                  

                  查看Coliru 现场演示

                  See a Live demo On Coliru

                  输出

                  22 serialization::archive 10 0 0 0 0 0 0 6 0 0 0 1 1 0
                  0 1 0
                  1 0 10 cow-jumped 6 1
                  2
                  3 0 4 moon 5 1
                  4
                  5 0 4 lazy 4 1
                  6
                  7 0 3 the 3 1
                  8
                  9 0 5 world 2 1
                  10
                  11 0 5 hello 1 0
                  

                  注意事项

                  • 我为 std::unordered_map 定义了序列化,因此您不再需要使用已弃用的 GNU 库扩展(另请参阅 这个错误报告/补丁)
                  • 我注释掉了 _total_alloc 因为,当然,你不希望这个数字被反序列化
                  • 我将管理 Item 的生命周期/分配作为练习(我不知道您希望如何组织所有权语义)
                  • I defined serialization for std::unordered_map so you no longer have to use deprecated GNU library extensions (see also this bug-report/patch)
                  • I commented out the _total_alloc because, surely, you don't want this number de-serialized
                  • I left managing the lifetime/allocation of Items as an exercise (I don't know how you wanted ownership semantics organized)
                  #include <boost/archive/binary_oarchive.hpp>
                  #include <boost/archive/binary_iarchive.hpp>
                  #include <boost/archive/text_oarchive.hpp>
                  #include <boost/archive/text_iarchive.hpp>
                  
                  #include <unordered_map>
                  #include <boost/serialization/collections_save_imp.hpp>
                  #include <boost/serialization/collections_load_imp.hpp>
                  #include <boost/serialization/utility.hpp>
                  #include <boost/serialization/split_free.hpp>
                  
                  namespace boost { namespace serialization {
                  
                      template<class Archive, typename... TArgs >
                          inline void save(Archive & ar, std::unordered_map<TArgs...> const&t, unsigned) {
                              boost::serialization::stl::save_collection<Archive, std::unordered_map<TArgs...> >(ar, t);
                          }
                  
                      template<class Archive, typename... TArgs >
                          inline void load(Archive & ar, std::unordered_map<TArgs...> &t, unsigned) {
                              boost::serialization::stl::load_collection<Archive,
                                  std::unordered_map<TArgs...>,
                                  boost::serialization::stl::archive_input_map<
                                      Archive, std::unordered_map<TArgs...> >,
                                  boost::serialization::stl::no_reserve_imp<std::unordered_map<TArgs...> >
                                      >(ar, t);
                          }
                  
                      // split non-intrusive serialization function member into separate
                      // non intrusive save/load member functions
                      template <class Archive, typename... TArgs>
                          inline void serialize(Archive & ar, std::unordered_map<TArgs...> &t, unsigned file_version) {
                              boost::serialization::split_free(ar, t, file_version);
                          }
                  } }
                  
                  #include <boost/serialization/base_object.hpp>
                  
                  class StringItem;
                  
                  class Item {
                    protected:
                      unsigned int _refc;
                      static unsigned int _total_alloc;
                  
                      //other code
                      Item() : _refc(0) { }
                      virtual ~Item() {}
                  
                    private:
                      friend class boost::serialization::access;
                  
                      template<class Archive>
                          void serialize(Archive & ar, unsigned)
                      {
                          ar & _refc;
                          //ar & _total_alloc; // wut? a static?!
                      }
                  };
                  
                  /*static*/ unsigned int Item::_total_alloc;
                  
                  class StringItem : public Item {
                    public:
                      StringItem(std::string s = "") : _s(std::move(s)) { }
                    private:
                      std::string _s;
                  
                      friend class boost::serialization::access;
                      template<class Archive>
                          void serialize(Archive & ar, unsigned)
                          {
                              ar & boost::serialization::base_object<Item>(*this);    
                              ar & _s;
                          }
                  };
                  
                  
                  struct Model {
                      virtual ~Model() {}
                      template<class Archive> void serialize(Archive&r, unsigned) { }
                  };
                  
                  class TokenFinder : public Model
                  {
                    public:
                      TokenFinder(void) : _element_count(0) {}
                  
                      virtual ~TokenFinder(void) {}
                  
                      virtual void insert_item(Item *item) { _elements[item] = _elements.size()+1; }
                  
                    private:
                      /** Map to store tokens together with their number of occurrences. */
                      std::unordered_map<Item*, unsigned long> _elements;
                      unsigned long _element_count;
                  
                      friend class boost::serialization::access;
                      template<class Archive>
                          void serialize(Archive & ar, unsigned)
                          {
                              ar.template register_type<StringItem>();
                              ar & boost::serialization::base_object<Model>(*this);    
                              ar & _elements;
                              ar & _element_count;
                          }
                  };
                  
                  int main()
                  {
                      boost::archive::text_oarchive oa(std::cout);
                  
                      std::vector<StringItem> seed_data {
                          {"hello"},{"world"},{"the"},{"lazy"},{"moon"}, {"cow-jumped"} 
                      };
                  
                  
                  
                      TokenFinder tf;
                      for(auto& si : seed_data)
                          tf.insert_item(&si);
                  
                      oa << tf;
                  
                  }
                  

                  这篇关于以自定义对象为键的 hash_map 的 C++ Boost.Serialization 错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  How to limit the number of running instances in C++(C++中如何限制运行实例的数量)
                  Using boost::asio::async_read with stdin?(将 boost::asio::async_read 与 stdin 一起使用?)
                  How to find out what dependencies (i.e other Boost libraries) a particular Boost library requires?(如何找出特定 Boost 库需要哪些依赖项(即其他 Boost 库)?)
                  What#39;s the purpose of a leading quot;::quot; in a C++ method call(引导“::的目的是什么?在 C++ 方法调用中)
                  Boost Spirit x3: parse into structs(Boost Spirit x3:解析为结构体)
                  How boost auto-linking makes choice?(boost自动链接如何做出选择?)
                1. <tfoot id='yXaxK'></tfoot>

                      • <bdo id='yXaxK'></bdo><ul id='yXaxK'></ul>
                      • <legend id='yXaxK'><style id='yXaxK'><dir id='yXaxK'><q id='yXaxK'></q></dir></style></legend>

                        <i id='yXaxK'><tr id='yXaxK'><dt id='yXaxK'><q id='yXaxK'><span id='yXaxK'><b id='yXaxK'><form id='yXaxK'><ins id='yXaxK'></ins><ul id='yXaxK'></ul><sub id='yXaxK'></sub></form><legend id='yXaxK'></legend><bdo id='yXaxK'><pre id='yXaxK'><center id='yXaxK'></center></pre></bdo></b><th id='yXaxK'></th></span></q></dt></tr></i><div id='yXaxK'><tfoot id='yXaxK'></tfoot><dl id='yXaxK'><fieldset id='yXaxK'></fieldset></dl></div>
                            <tbody id='yXaxK'></tbody>

                            <small id='yXaxK'></small><noframes id='yXaxK'>