提升反序列化错误

时间:2014-07-07 01:31:07

标签: c++ boost boost-serialization

我正在尝试序列化/反序列化一个简单的对象。 我能够将其序列化:

#include <vector>
   #include <sstream>
   #include <boost/archive/text_oarchive.hpp>
   #include <boost/archive/text_iarchive.hpp>
   #include <boost/serialization/vector.hpp>


   class DataClass{
       public:
           std::vector<std::string> data;

           DataClass(){}
           ~DataClass(){}

           friend class boost::serialization::access;
           template<typename Archive>
               void serialize(Archive & ar, const unsigned int version) const{
                   ar & data;
               }   
   };

   int main(){
       using std::cout;
       using std::endl;
       using std::string;

       DataClass data_obj;
       data_obj.data.push_back("some data 1");
       data_obj.data.push_back("some data 2");

       std::ostringstream archive_stream;
       boost::archive::text_oarchive archive(archive_stream);
       archive << data_obj;
       string str_data_to_send = archive_stream.str();
       const char* data_to_send = archive_stream.str().c_str();


       std::istringstream archive_stream2(data_to_send);

       boost::archive::text_iarchive archive2(archive_stream2);
       DataClass received_data_obj;
       //archive2 >> received_data_obj;

   }

如果我取消注释最后一个语句

,我会收到错误

archive2&gt;&gt; received_data_obj;

In file included from /opt/local/include/boost/archive/text_oarchive.hpp:31:
In file included from /opt/local/include/boost/archive/basic_text_oarchive.hpp:32:
In file included from /opt/local/include/boost/archive/detail/common_oarchive.hpp:22:
In file included from /opt/local/include/boost/archive/detail/interface_oarchive.hpp:23:
In file included from /opt/local/include/boost/archive/detail/oserializer.hpp:67:
/opt/local/include/boost/archive/detail/check.hpp:162:5: error: static_assert failed "typex::value"
    BOOST_STATIC_ASSERT(typex::value);
    ^                   ~~~~~~~~~~~~

我无法发布整个错误消息,因为我的帖子将是&#34;主要是代码&#34;。

1 个答案:

答案 0 :(得分:4)

转到发生静态断言的源代码,您将看到解释该问题的注释:

// cannot load data into a "const" object unless it's a
// wrapper around some other non-const object.

这是因为您将serialization成员函数定义为const,因此data成员在const函数中访问时也是serialization