1

我正在尝试构建一个树状结构来进行由一系列问题组成的测试。

这个想法是 Test、Question、QuestionPart 都将派生自以下类:

class Node
{    
    friend class boost::serialization::access;
    template<class Archive>
    void serialize(Archive & ar, const unsigned int version)
    {
        ar & boost::serialization::make_nvp("children", m_children);
        ar & boost::serialization::make_nvp("intro", m_intro);
    }

public:
    Node(){};   
    virtual ~Node(){};

    virtual void addChildNode( Node* _child );      
    virtual const std::string getIntro() const;
    virtual Node* getChild( const _index ) const;
    virtual void setIntro( std::string _intro );

protected:
    std::vector<Node*> m_children;
    std::string m_intro;
};

在做所有其他课程之前,我尝试了以下内容:

void saveTest( const Node &test, const char* filename)
{
    // make an archive
    std::ofstream ofs(filename);
    boost::archive::xml_oarchive oa(ofs);
    oa << BOOST_SERIALIZATION_NVP(test);
}

void createTest()
{
    Node* test = new Node();
    Node* question1 = new Node();

    question1->setIntro("Question 1");

    test->addChildNode(question1);
    test->setIntro("Test");

    saveTest( *test, fileLocation.c_str() );
}

但这会引发异常,并且只会创建以下内容:

<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<!DOCTYPE boost_serialization>
<boost_serialization signature="serialization::archive" version="10">
<test class_id="0" tracking_level="1" version="0" object_id="_0">
<children class_id_reference="0" object_id="_1">
    <children</boost_serialization>

任何见解将不胜感激。

4

1 回答 1

2

事实证明这是一个简单的修复;在这里看到了一个例子,我改变了以下内容:

std::vector<Node*> m_children;

boost::ptr_vector<Node> m_children;

这需要以下内容:

#include <boost/ptr_container/serialize_ptr_vector.hpp>

但是,我不确定为什么会这样(可能没有跟踪对象?),所以欢迎解释!

于 2013-05-17T13:25:41.617 回答