-
April 15th, 2014, 01:03 PM
#1
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.
-
April 15th, 2014, 01:13 PM
#2
Re: boost serialization of nested struct does not work in c++
Before posting please format the code properly and use code tags so that the code is readable. Go Advanced, select the formatted code and click '#'.
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
April 15th, 2014, 08:52 PM
#3
Re: boost serialization of nested struct does not work in c++
-
April 17th, 2014, 04:37 AM
#4
Re: boost serialization of nested struct does not work in c++
Originally Posted by manishak
edited the code..
Could you please fix the compiler errors, such that people can just copy-paste the code if they want to run it?
What about the serialization "does not work"?
Cheers, D Drmmr
Please put [code][/code] tags around your code to preserve indentation and make it more readable.
As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|