我有一个需要序列化的树类。编码:
#include <string>
#include <boost/serialization/vector.hpp>
#include <boost/serialization/string.hpp>
#include <boost/serialization/access.hpp>
#include <boost/serialization/tracking.hpp>
using namespace std;
class AVLtree {
public:
string name;
int fid;
int p1;
int n1;
double ig;
AVLtree *left; // left subtree
AVLtree *right; // right subtree
int height; // height of the tree
long TotalNodes;
};
BOOST_CLASS_TRACKING(AVLtree, track_always)
namespace boost {
namespace serialization {
template<class Archive>
void serialize(Archive &ar, AVLtree &tree, const unsigned int version) {
ar & tree.name;
ar & tree.fid;
ar & tree.p1;
ar & tree.n1;
ar & tree.ig;
ar & tree.height;
ar & tree.TotalNodes;
ar & *tree.left; // Haven't yet tried it with *tree.left, but just tree.left saves the memory address, not the object
ar & *tree.right;
} // end serialize()
} // end namespace serialization
} // end namespace boost
我在网上查看了很多其他评论和代码示例,包括这个站点和 Boost 文档,但我不知道如何处理这样的递归情况。其中类包含相同类型的对象的两个指针。我应该如何修改树或序列化函数以使其工作?谢谢你。