2013-05-17 2 views
1

일련의 질문으로 구성된 테스트를 유지하기 위해 구조와 같은 트리를 작성하려고합니다.부스트 직렬화 트리 구조

는, 질문, QuestionPart 모두 다음 클래스에서 해당 시험 아이디어를 도출 것입니다 :

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

그러나 이것이다 :

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

다른 모든 클래스를 수행하기 전에 나는 다음 밖으로 시도 예외를 던지고 다음 만 생성 :

<?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> 

임의의 insigh t는 매우 감사 할 것이다.

+0

부스트 property_tree 사용을 고려 했습니까? – ppl

+0

제가 언급 한 것을 보았습니다. 한번 살펴 보겠습니다. 귀하의 제안에 감사드립니다. – henryprescott

답변

1

간단한 수정으로 밝혀졌습니다. 예를 들어 here을 본 데, 나는 다음과 같은 변경 :

std::vector<Node*> m_children; 

다음이 필요

boost::ptr_vector<Node> m_children; 

에 포함 :

#include <boost/ptr_container/serialize_ptr_vector.hpp> 

을하지만,이 작품 이유를 잘 모르겠어요 (개체가 추적되지 않을 수 있습니다?), 그래서 설명을 환영합니다!