C++ shared_ptr serialization

C++ shared_ptr serialization

本文关键字:serialization ptr shared C++      更新时间:2023-10-16

我有一个名为 A 的类,我想在另一个名为 B 的类中序列化它的对象,但我不断收到此错误:

error: ‘class std::shared_ptr<A>’ has no member named ‘serialize’

A类是:

class A
{
public:
  typedef shared_ptr<A> Ptr;
  string name;
  Predicate(const string &name = ""):name(name)
  {}
private:
  template<typename Archive>
  void serialize(Archive& archive, const unsigned int v) 
  {
    archive & name;
  }
  friend class B;
  friend class boost::serialization::access;
}

B类:

class B
{
public:
  typedef unordered_set<A::Ptr, 
                        APtrKeyHash, 
                        APtrKeyEq> A_set_t;
  A_set_t test;
private:
  template<typename Archive>
  void serialize(Archive& archive, const unsigned int v) 
  {
    archive & test;
  }
  friend class boost::serialization::access;
}

请注意,这里shared_ptr是指 std::shared_ptr,而不是 boost::shared_ptr。 事实上,我使用了这一行: 使用命名空间 std; 在我的 A 类之前

你可能忘了包含

#include <boost/serialization/shared_ptr.hpp>

您需要 shared_ptr.hpp 和 boost 版本 1.56 的组合