CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Threaded View

  1. #1
    Join Date
    Apr 2014
    Posts
    4

    boost serialization of nested struct does not work in c++

    I'm trying to serialize a struct which contains two or more other types of sturct. But it seems that serialization does not work. Below is sampel code :

    /SimpleData.hpp

    Code:
     #include <boost/serialization/access.hpp>
        #include <boost/serialization/base_object.hpp> 
        #include <boost/serialization/optional.hpp> 
        #include <boost/serialization/map.hpp> 
        #include <boost/serialization/shared_ptr.hpp>
        
        namespace A
        {
        
        Struct Name
        {
        	std::string firstname;
        	std::string lastname;
        
              template<class Archive>
              void serialize(Archive &ar, const unsigned int version)
              {
                 ar & firstname;
                 ar & lastname;
              }	
        };
        
        Struct Address
        {
           std::string street;
           std::string city;
        
              template<class Archive>
              void serialize(Archive &ar, const unsigned int version)
              {
                 ar & street;
                 ar & city;
              }	
        
        }
        
        Struct FullData 
        {
        
          public :
              FullData();
              FullData(const parameter_strings & parms);
         
              virtual ~FullData();
        
              boost::optional<Name> nameinfo;       // instance of struct Name
              boost::optional<Address> addressinfo;    // instance of struct Address
        
              std::string country;
              int pincode;
        
          private :
              friend class boost::serialization::access;
              template<class Archive>
              void serialize(Archive &ar, const unsigned int version)
              {
                 ar & nameinfo;       // are these enough to take of serialization of Name and 
                 ar & addressinfo;   // Address
                 ar & country;;
                 ar & pincode;
              }	
        };
        
        }
    //SimpleData.cpp


    #include "SimpleData.hpp"

    FullData::FullData()
    {

    }

    Code:
    FullData::~FullData()
        {
        
        }
        
        FullData::~FullData(const parameter_strings & parms)
        {
        
         // impelmentation
        }
        
        
        BOOST_CLASS_EXPORT_IMPLEMENT(FullData);
        BOOST_CLASS_IMPLEMENTATION(FullData),boost::serialization::object_serializable);
        BOOST_CLASS_TRACKING(FullData),boost::serialization::track_never);
    I'm not sure if above code would serialize properly or not. Whats wrong with above code.
    Do i need to add below code also for serialization of Name and Address struct in SimpleData.cpp file

    Code:
      BOOST_CLASS_EXPORT_IMPLEMENT(Name);
        BOOST_CLASS_IMPLEMENTATION(name,boost::serialization::object_serializable);
        BOOST_CLASS_TRACKING(Name,boost::serialization::track_never);
        
        
        BOOST_CLASS_EXPORT_IMPLEMENT(Address);
        BOOST_CLASS_IMPLEMENTATION(Address,boost::serialization::object_serializable);
        BOOST_CLASS_TRACKING(Address,boost::serialization::track_never);
    Thanks
    Last edited by manishak; April 15th, 2014 at 08:52 PM.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured