指向std :: list类型的共享指针的赋值不起作用

时间:2014-05-05 08:08:45

标签: c++ serialization boost boost-serialization

我试图为指向std :: list的共享指针赋值,但是assign失败并给出错误,下面是我的代码供参考:

struct X
{
   public:
      X();
      X(int a,int b, std::string ss)
      {
         y=a;
         z=b;
         aa=ss;
      }

      int y;
      int z;
      std::string aa;
      typedef std::list<X> Xdata;

   private:
      friend class boost::serialization::access;
      template<class Archive>
      void serialize(Archive &at, const unsigned int version)
      {
         at & y;
         at & z;
         at & aa;
      }  
};

struct Q
{
    public:
       Q();
       std::string q1;
       boost::shared_ptr<X::Xdata> xx;

     private:
      friend class boost::serialization::access;
      template<class Archive>
      void serialize(Archive &at, const unsigned int version)
      {
         at & q1;
         at & xx;   // will this serialize ??
      } 
};


void function_assignvalue(Q *e)
{
    e->q1 = "hello";

    X datax;
    datax.y=10;
    datax.z=20;
    datax.aa="x";

    e->xx.reset(new X::Xdata());  // gives error
}

它会给出一些未解析的符号错误,还会xx序列化吗?

2 个答案:

答案 0 :(得分:1)

为您的XQ类提供默认构造函数(请参阅下面的代码):

struct X
{
   public:
      X() : a(0), b(0) {}
      X(int a,int b, std::string ss) : y(a), z(b), aa(ss) {}

      int y;
      int z;
      std::string aa;
      typedef std::list<X> Xdata;

   private:
      friend class boost::serialization::access;
      template<class Archive>
      void serialize(Archive &at, const unsigned int version)
      {
         at & y;
         at & z;
         at & aa;
      }  
};

struct Q
{
    public:
       Q() {}
       std::string q1;
       boost::shared_ptr<X::Xdata> xx;

     private:
      friend class boost::serialization::access;
      template<class Archive>
      void serialize(Archive &at, const unsigned int version)
      {
         at & q1;
         at & xx;   // will this serialize ??
      } 
};

答案 1 :(得分:1)

#include <boost/serialization/shared_ptr.hpp>
#include <boost/serialization/list.hpp>

将序列化。

对于未解析的符号,您需要定义它们并链接定义它们的对象。另见FAQ条目:

相关问题