boost::archive_exception
令我感到困惑。例外情况如下
Unhandled exception: boost::archive::archive_exception at memory location...
尝试执行以下boost::serialization
加载操作时会出现这种情况,该操作基本上适用于BinarySearchTree<std::string>
但不适用于BinarySearchTree<int>
typedef boost::variant<BinarySearchTree<std::string>, BinarySearchTree<int>> BinarySearchTreeVariant;
BinarySearchTreeVariant search_tree_;
BinarySearchTree<std::string> string_search_tree_;
BinarySearchTree<int> int_search_tree_;
boost::shared_ptr<BinarySearchTree<std::string>> binary_string_search_tree_;
boost::shared_ptr<BinarySearchTree<int>> binary_int_search_tree_;
search_tree_ = int_search_tree_;
binary_int_search_tree_.reset(new BinarySearchTree<int>());
try{
if(!binary_int_search_tree_->load(binary_int_search_tree_, search_tree_name))
throw CustomException("Load of binary search tree to disk fail");
}
catch(CustomException &custom_exception){ }
定义load
的代码如下
bool load(boost::shared_ptr<BinarySearchTree> &tree, const std::string &search_tree_file_name)
{
// create and open an archive for output
std::ifstream reader(search_tree_file_name);
if(reader){
boost::archive::text_iarchive serial_reader(reader);
// read class state from archive
serial_reader >> *tree; <<<--- Unhandled Exception
// archive and stream closed when destructors are called
}else if(reader.fail()){
reader.clear();
}
return true;
}
我的search tree
定义为boost::serialization
,如下所示
class BinarySearchTree{
private:
class BinarySearchTreeNode{
public:
friend class BinarySearchTree;
private:
friend class boost::serialization::access;
template<class Archive>
void serialize(Archive &archive, const unsigned int /* file_version */){
archive & key;
archive & index;
archive & left;
archive & right;
}
T key;
long index;
boost::shared_ptr<BinarySearchTreeNode> left;
boost::shared_ptr<BinarySearchTreeNode> right;
}; // End Tree Node Class Definition
boost::shared_ptr<BinarySearchTreeNode> root;
friend class boost::serialization::access;
template <class Archive>
void serialize(Archive &archive, const unsigned int version){
archive & root;
}
};