Search code examples
c++serializationboostshared-ptr

C++ shared_ptr serialization


I have a class named A which I want to serialize it's object in another class named B. but I keep getting this error:

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

class A is:

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

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

note that by shared_ptr here I mean std::shared_ptr, not boost::shared_ptr. in fact I used this line: using namespace std; before my A class


Solution

  • You probably forgot to include

    #include <boost/serialization/shared_ptr.hpp>