my intention is to get input from map<int,std::string>
and create the binary archive which the below code is failing to do , is this the right way of creating it?
And how to know whether the data is correctly archived or not(desirializing using boost)
Do i just need to create a boost::archive::binary_iarchive oa(oss,1);
and copy it to oss ?
#include <fstream>
#include <boost/serialization/map.hpp>
#include <boost/serialization/nvp.hpp>
#include <boost/archive/xml_oarchive.hpp>
#include <string>
#include <iostream>
#include <map>
#include <boost/archive/binary_oarchive.hpp>
#include <boost/archive/binary_iarchive.hpp>
#include <boost/serialization/serialization.hpp>
using namespace std;
class MyConnections
{
public:
MyConnections()
{
e_group.insert( std::make_pair(1,"ETOTO") ) ;
e_group.insert( std::make_pair(2,"ETOTO") ) ;
}
template<class archive>
void serialize(archive& ar, const unsigned int version)
{
using boost::serialization::make_binary_object;
ar & boost::serialization::make_binary_object(e_group);
}
public:
typedef map<int,std::string> groups;
groups e_group;
};
int main(int argc, char* argv[])
{
const MyConnections conn;
stringstream oss(ios_base::out|ios_base::binary);
boost::archive::binary_oarchive oa(oss,1);
oa << boost::serialization::make_binary_object(conn)
std::cout<<oss.str;
}
Error
main.cpp: In member function 'void MyConnections::serialize(archive&, unsigned int)':
main.cpp:29:63: error: cannot convert 'MyConnections::groups {aka std::map<int, std::__cxx11::basic_string<char> >}' to 'void*' for argument '1' to 'const boost::serialization::binary_object boost::serialization::make_binary_object(void*, std::size_t)'
ar & boost::serialization::make_binary_object(e_group);
^
main.cpp: In function 'int main(int, char**)':
main.cpp:42:55: error: cannot convert 'const MyConnections' to 'void*' for argument '1' to 'const boost::serialization::binary_object boost::serialization::make_binary_object(void*, std::size_t)'
oa << boost::serialization::make_binary_object(conn)
^
Std output doesn't support random binary output. You'd probably use hex of base64 encoding.
Here's a simplistic approach writing out hex bytes:
Prints:
See it Live On Coliru