1

Aboost::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;
    }
};
4

0 回答 0