static_assert失败" typex :: value"使用boost

时间:2017-03-29 10:14:29

标签: c++ boost boost-serialization

下面的代码将类和结构的成员序列化和反序列化。 序列化工作正常但我在尝试使用oarch >> BOOST_SERIALIZATION_NVP(outObj); 反序列化时遇到了以下错误 代码中有没有大错过,我还没有实现,也没有实现。

  

main.cpp中包含的文件:1:   在/usr/local/include/boost/archive/binary_oarchive.hpp:21中包含的文件中:   在/usr/local/include/boost/archive/binary_oarchive_impl.hpp:22中包含的文件中:   在/usr/local/include/boost/archive/basic_binary_oarchive.hpp:33中包含的文件中:   在/usr/local/include/boost/archive/detail/common_oarchive.hpp:22中包含的文件中:   在/usr/local/include/boost/archive/detail/interface_oarchive.hpp:23中包含的文件中:   在/usr/local/include/boost/archive/detail/oserializer.hpp:68中包含的文件中:   /usr/local/include/boost/archive/detail/check.hpp:162:5:错误:static_assert失败" typex :: value"       BOOST_STATIC_ASSERT(而TYPEx ::值);       ^ ~~~~~~~~~~~~   /usr/local/include/boost/static_assert.hpp:70:41:注意:从宏扩展' BOOST_STATIC_ASSERT'

#include <boost/archive/binary_oarchive.hpp>
#include <boost/archive/xml_oarchive.hpp>
#include <boost/archive/binary_iarchive.hpp>
#include <boost/serialization/binary_object.hpp>
#include <boost/serialization/serialization.hpp>
#include <boost/serialization/nvp.hpp>
#include <iostream>
#include <string>
#include <iomanip>
#include <sstream>
#include <fstream>

#include <map>
#include <boost/serialization/map.hpp>
#include <boost/serialization/split_member.hpp>

struct values
{
    std::string name;
    std::string sex;
    values():name("dummy"),sex("dummy"){} ;

    template<class Archive>   
    void serialize(Archive & ar, const unsigned int version)
    {
        ar & BOOST_SERIALIZATION_NVP(name);
        ar & BOOST_SERIALIZATION_NVP(sex);
    }
};

class Myclass
{
public:

    Myclass()
    {
        values val1;
        e_group.insert( std::make_pair(1,val1) ) ;
        e_group.insert( std::make_pair(2,val1) ) ;
        p_group.insert( std::make_pair(1,e_group) ) ;
        p_group.insert( std::make_pair(2,e_group) ) ;
    }

    template<class Archive>
    void serialize(Archive & ar, const unsigned int version) const
    {
        ar & BOOST_SERIALIZATION_NVP(e_group);
        ar & BOOST_SERIALIZATION_NVP(p_group);
    }

    typedef   std::map<int,values> groups;
    typedef   std::map<int,groups> Pgroups;
    groups  e_group;
    Pgroups p_group;
};



int main()
{
    Myclass assetlist;

    auto os = std::ostringstream(std::ios::binary);

    boost::archive::binary_oarchive arch( os, boost::archive::no_header);

    arch << BOOST_SERIALIZATION_NVP(assetlist);

    std::string s1 = os.str();

    std::stringstream is( s1, std::ios_base::binary| std::ios_base::out| std::ios_base::in);

    Myclass outObj;

    boost::archive::binary_iarchive iarch (is , boost::archive::no_header );

    iarch >> BOOST_SERIALIZATION_NVP(outObj);

    return 0; 
 } 

1 个答案:

答案 0 :(得分:4)

你的静态断言失败了,因为额外的&#39; const&#39;在Myclass :: serialize函数中。 它应该看起来像:

template<class Archive>
    void serialize(Archive & ar, const unsigned int version) 
    { ... }

保留BOOST_SERIALIZATION_NVP()用法,请参阅this问题。