c++ boost元组序列化/反序列化



这可能真的很简单,但我是股票。我正在尝试序列化和反序列化boost::tuple。我写了一个"serialize"函数,它完美地序列化了元组。但是,我没有任何"反序列化"功能。我甚至不确定我是否需要一个。大多数例子甚至都没有。这是我的代码,

namespace boost
{
    namespace serialization
    {
        template<typename Archive, typename T1, typename T2>
        void serialize(Archive & ar, boost::tuple<T1, T2> & t,
                    const unsigned int)
        {
            ar & t.get<0>();
            ar & t.get<1>();
        }
    }
}
using namespace std;
using namespace boost;
int main() {
    std::ofstream os("archive.txt");
    {
        boost::tuple<int, int> t = boost::make_tuple(1, 5);
        boost::archive::text_oarchive ar(os);
        ar & boost::serialization::make_nvp("first", t.get<0>() );
        ar & boost::serialization::make_nvp("second", t.get<1>() );
    }
    {
        std::ifstream file("archive.txt");
        boost::archive::text_iarchive ia(file);
        boost::tuple<int, int> t;
            ia >> t;
            std::cout << "First: " << t.get<0>() << "n" << "Second: " << t.get<1>() << std::endl;
    }
    return 0;
}

我得到下面的错误,

Undefined symbols for architecture x86_64: "boost::archive::text_iarchive_impl
<boost::archive::text_iarchive>::load_override(boost::archive::class_name_type&, int)", referenced from: boost::archive::text_iarchive& boost::archive::detail::interface_iarchive
  <boost::archive::text_iarchive>::operator>>
    <boost::archive::class_name_type>(boost::archive::class_name_type&) in cc4Y8n8F.o ld: symbol(s) not found for architecture x86_64 collect2: error: ld returned 1 exit status

如何克服这个错误?

boost::archive::text_iarchive ia(file);行导致错误

需要在依赖类型中限定模板成员:

        ar & t.template get<0>();
        ar & t.template get<1>();

参见在哪里以及为什么我必须把"template"one_answers";typename"关键字?

或者,使用自由函数get:

 ar & get<0>(t) & get<1>(t);

最新更新