boost :: archive_exception导致Visual Studio 2012中出现未处理的异常

时间:2013-05-06 19:18:21

标签: c++ unhandled-exception boost-serialization

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;
    }
};

0 个答案:

没有答案