2010-07-19 4 views
2

아래의 직렬화 예제는 boost mailing list에서 가져온 것입니다. 이것은 내가하고 싶은 것과 거의 같습니다. 그러나 XML로 직렬화되도록 아카이브를 변경했습니다. 바이너리로 직렬화하면 컴파일이 실패하지 않지만 xml로 직렬화하면 컴파일이 실패합니다. 컴파일은 다음과 같은 방법으로 basic_xml_oarchive.hpp 실패 :이 문제를 해결하는 방법에 대한boost :: serialization을 사용하여 객체지도를 xml로 serialize

// boost code where compile fails 
template<class T> 
void save_override(T & t, BOOST_PFTO int) 
{ 
    // If your program fails to compile here, its most likely due to 
    // not specifying an nvp wrapper around the variable to 
    // be serialized. 
    BOOST_MPL_ASSERT((serialization::is_wrapper<T>)); 
    this->detail_common_oarchive::save_override(t, 0); 
} 

내가 std::map<int, CSomeData> 객체 직렬화 할 수 있도록 충분히 수행하지 않은 것, 어떤 아이디어?


내 직렬화 구현 :

#include <boost/archive/xml_oarchive.hpp> 
#include <boost/archive/xml_iarchive.hpp> 
#include <boost/serialization/map.hpp> 
#include <fstream> 
#include <string> 
#include <map> 

using namespace std; 

// This is a test class to use as the map data. 
class CSomeData { 
    public: 
     CSomeData(){}; 
     CSomeData(float f0, string str0) 
     { 
      m_f0 = f0; 
      m_str0 = str0; 
     } 

     float m_f0; 
     string m_str0; 

    private: 
     friend class boost::serialization::access; 

     template<class Archive> 
     void serialize(Archive &ar, const unsigned int version) 
     { 
      ar & m_f0; 
      ar & m_str0; 
     } 
}; 

// This is the class we really want to try serializing. 
class CTest { 
    public: 
     CTest(){}; 
     CTest(int nNumber) 
     { 
      m_nNumber = nNumber; 

      // Fill with some dummy data. 
      m_mTst.insert(make_pair(0, CSomeData(0.23f, "hi hi hi"))); 
      m_mTst.insert(make_pair(1, CSomeData(7.65f, "second one"))); 
      m_mTst.insert(make_pair(2, CSomeData(9.23f, "third one"))); 
      m_mTst.insert(make_pair(3, CSomeData(5.6766, "chosen one"))); 
     } 
     ~CTest(){}; 

     save() 
     { 
      std::ofstream ofs("filename"); 

      // Write class instance to archive. Writing seems to work ok. 
      boost::archive::xml_oarchive oa(ofs); 
      oa << BOOST_SERIALIZATION_NVP(*this); 
     } 

     int m_nNumber; 

    private: 
     map<int, CSomeData> m_mTst; 

     friend class boost::serialization::access; 

     template<class Archive> 
     void serialize(Archive &ar, const unsigned int version) 
     { 
      ar & m_nNumber; 
      ar & m_mTst; 
     } 
}; 
+0

내 문제는 코드기어에서 지원되지 않습니다 그 직렬화라고 생각합니다. http://blogs.embarcadero.com/ddean/2009/09/23/34847 – Seth

답변

4

난 당신이 XML 직렬화의 이름으로 회원 태그를 추가해야 믿습니다. 이것은 XML에서 사용할 요소 이름을 지정합니다. 나는. (이 경우 더 나은)

ar & BOOST_SERIALIZATION_NVP(m_f0); 

나 :

ar & make_nvp("field0", my_f0); 

태그는 바이너리 직렬화 무시됩니다 같은 것을 사용합니다. 여기 자세한 내용은 :

http://www.boost.org/doc/libs/1_43_0/libs/serialization/doc/wrappers.html

+0

작동하지 않았고 이미'BOOST_SERIALIZATION_NVP' 매크로를 사용하고있었습니다 – Seth

+0

CSomeData :: serialize()는 위의 코드에 없습니다. . –

+0

네 말이 맞아! 그것도 컴파일. – Seth

관련 문제