gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 13:28:47 24 4
gpt4 key购买 nike

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

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>();

另见 Where and why do I have to put the "template" and "typename" keywords?

或者,使用免费函数 get:

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

关于C++ boost元组序列化/反序列化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32327899/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com