gpt4 book ai didi

C++ Boost - 包含类层次结构对象的类的序列化

转载 作者:行者123 更新时间:2023-12-03 12:48:09 26 4
gpt4 key购买 nike

我有课A ,其中包含类 B 的对象我想将其序列化。问题是,类 C继承自B ,所以A可以包含 B 的对象或C 。如何使用Boost高效地实现序列化?

我的尝试如下,但在尝试序列化 A 时遇到错误与 C对象,而 B它工作正常。你知道我做错了什么吗?

我找到了一些有关类层次结构对象序列化的信息 here ,但它需要在 text_iarchive 中显式注册该类型,而我需要将其注册在 A 中类,因为我没有直接序列化 B对象。

<小时/>

我的尝试:

#include <fstream>
#include <iostream>
#include <vector>
#include <boost/archive/text_oarchive.hpp>
#include <boost/archive/text_iarchive.hpp>

class B {
friend class boost::serialization::access;

private:
template<class Archive>
void save(Archive & ar, const unsigned int version) const {
ar & this->v->size();
for(int i = 0; i < this->v->size(); i++) {
ar & (*(this->v))[i];
}
};

template<class Archive>
void load(Archive & ar, const unsigned int version) {
size_t size;
int tmp;
ar & size;

this->v = new std::vector<int>(size);
for(int i = 0; i < size; i++) {
ar & tmp;
(*this->v)[i] = tmp;
}
}

BOOST_SERIALIZATION_SPLIT_MEMBER()

protected:
std::vector<int>* v;

public:
B();
B(std::vector<int>* v);
virtual void print_vals();
};

B::B() {
this->v = nullptr;
}

B::B(std::vector<int>* v) {
this->v = v;
}

void B::print_vals() {
for(auto e : *(this->v)) {
std::cout << e << std::endl;
}
}

class C : public B {
friend class boost::serialization::access;
private:
int num2;

template<class Archive>
void serialize(Archive & ar, const unsigned int version){
ar & boost::serialization::base_object<B>(*this);
ar & num2;
};

public:
void print_vals() override {
for(auto e : *(this->v)) {
std::cout << e << std::endl;
}
std::cout << this->num2 << std::endl;
}

C();
C(int num2, std::vector<int>* v);

};

C::C() {
this->num2 = -1;
this->v = nullptr;
}

C::C(int num2, std::vector<int> *v) {
this->num2 = num2;
this->v = v;
}

class A {
friend class boost::serialization::access;

private:
int num;

B* b_obj;

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

public:
A();
A(int num, B* b);

void print_vals();
};

A::A() {
this->num = -1;
this->b_obj = nullptr;
}

A::A(int num, B* b) {
this->num = num;
this->b_obj = b;
}

void A::print_vals() {
std::cout << this->num << std::endl;
this->b_obj->print_vals();
}


int main() {
std::vector<int> v{1,2,3};

B b = B(&v);

A a(4, &b);
std::cout << "a:" << std::endl;
a.print_vals();

std::ofstream ofs("a.txt");
{
boost::archive::text_oarchive oa(ofs);
oa << a;
ofs.close();
}

A a2;
std::ifstream ifs("a.txt");
{
boost::archive::text_iarchive ia(ifs);
ia >> a2;
ifs.close();
}
std::cout << "a2:" << std::endl;
a2.print_vals();

C c(2, &v);
A a3(6, &c);
std::cout << "a3:" << std::endl;
a3.print_vals();

std::ofstream ofs2("a3.txt");
{
boost::archive::text_oarchive oa(ofs2);
oa << a3;
ofs.close();
}

A a4;
std::ifstream ifs2("a3.txt");
{
boost::archive::text_iarchive ia(ifs2);
ia >> a4;
ifs.close();
}
std::cout << "a4:" << std::endl;
a4.print_vals();


}
<小时/>

输出:

a:
4
1
2
3
a2:
4
1
2
3
a3:
6
1
2
3
2
terminate called after throwing an instance of 'boost::archive::archive_exception'
what(): unregistered class - derived class not registered or exported
Signal: SIGABRT (Aborted)

最佳答案

事实证明,您错过了派生类的BOOST_CLASS_EXPORT,即

BOOST_CLASS_EXPORT(C)

如果没有此宏,Boost 序列化无法正确序列化指向派生对象的指针。

您可以找到完整的工作代码 here

关于C++ Boost - 包含类层次结构对象的类的序列化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51809104/

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