Boost logo

Boost Users :

Subject: [Boost-users] [serialization] stream error when reading simple tree structure from a binary archive
From: CurieCat (linfk_at_[hidden])
Date: 2010-03-13 00:10:39


Hi,
I tried to save a tree structure out to an archive and then read it back. If
I use a text archiver, everything works fine; If I use a binary archiver, an
exception of "stream error" is thrown. I managed to shorten my code as
following.

Can anyone give some advice? I'm using vs2008 and boost 1.42.0.
Thanks.

///////////////////////////////////////////////////////////////////////
#define _SCL_SECURE_NO_WARNINGS
#define _CRT_SECURE_NO_WARNINGS
#include <fstream>
#include <boost/archive/tmpdir.hpp>
#include <boost/archive/text_iarchive.hpp>
#include <boost/archive/text_oarchive.hpp>
#include <boost/archive/binary_iarchive.hpp>
#include <boost/archive/binary_oarchive.hpp>
#include <boost/serialization/base_object.hpp>
#include <boost/serialization/utility.hpp>
#include <boost/serialization/assume_abstract.hpp>
#include <boost/serialization/export.hpp>

class Base
{
    friend class boost::serialization::access;
    template<class Archive>
    void serialize(Archive & ar, const unsigned int version){}
public:
    virtual ~Base(){}
};
BOOST_CLASS_EXPORT_GUID(Base, "Base")

class Node : public Base
{
    friend class boost::serialization::access;
    template<class Archive>
    void serialize(Archive & ar, const unsigned int version)
    {
        ar & boost::serialization::base_object<Base>(*this);
        ar & parent_;
        ar & firstChild_;
        ar & nextSibling_;
    }
public:
    Node* parent_;
    Node* firstChild_;
    Node* nextSibling_;

    Node()
    {
        parent_ = NULL;
        firstChild_ = NULL;
        nextSibling_ = NULL;
    }

    virtual ~Node(){};

    bool AddChild_front(Node* node)
    {
        node->nextSibling_ = firstChild_;
        node->parent_ = this;
        firstChild_ = node;
        return true;
    }
};
BOOST_CLASS_EXPORT_GUID(Node, "Node")

class DeriveNode : public Node
{
    friend class boost::serialization::access;
    template<class Archive>
    void serialize(Archive & ar, const unsigned int version)
    {
        ar & boost::serialization::base_object<Node>(*this);
    }
};
BOOST_CLASS_EXPORT_GUID(DeriveNode, "DeriveNode")

int main()
{
    Node* root = new Node;
    Node* bodyNode = new Node;
    root->AddChild_front(bodyNode);
    Node* headNode = new Node;
    Node* Eye = new Node;
    Node* Nose = new Node;
    Node* Ear = new Node;
    headNode->AddChild_front( Eye);
    headNode->AddChild_front( Nose);
    headNode->AddChild_front( Ear);
    bodyNode->AddChild_front(headNode);
    Node* LeftArm = new Node;
    Node* RightArm = new Node;
    Node* LeftLeg = new Node;
    bodyNode->AddChild_front( LeftArm);
    bodyNode->AddChild_front( RightArm);
    bodyNode->AddChild_front( LeftLeg);

    DeriveNode* jointRoot = new DeriveNode;
    bodyNode->AddChild_front( jointRoot);
    DeriveNode* jointChild_A = new DeriveNode;
    jointRoot->AddChild_front( jointChild_A);
    DeriveNode* jointChild_B = new DeriveNode;
    jointRoot->AddChild_front( jointChild_B);

    const Node* rootnode = root ;
    Node* newroot = NULL;
    std::ofstream ofs("filename");
    {
        boost::archive::binary_oarchive oa(ofs);
        oa << rootnode;
    }
    ofs.close();
    std::ifstream ifs("filename");
    {
        boost::archive::binary_iarchive ia(ifs);
        ia >> newroot;
    }
    ifs.close();
    return 0;
}


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net