C++ shared_ptr 序列化

Ima*_*ami 2 c++ serialization boost shared-ptr

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

error: ‘class std::shared_ptr<A>’ has no member named ‘serialize’
Run Code Online (Sandbox Code Playgroud)

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;
}
Run Code Online (Sandbox Code Playgroud)

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;
}
Run Code Online (Sandbox Code Playgroud)

请注意,这里的 shared_ptr 是指 std::shared_ptr,而不是 boost::shared_ptr。事实上,我使用了这一行:using namespace std; 在我的 A 课之前

seh*_*ehe 5

你可能忘了包括

#include <boost/serialization/shared_ptr.hpp>
Run Code Online (Sandbox Code Playgroud)