dimanche 26 mars 2017

writing a map of structures to xml using boost

i could successfully write a map < string, string > to xml file using boost serialization but while i tried writing a map < string, struct values > to xml file using boost serialization , its fails

Both the code snippets are below

class MyConnections
{
 public:
 MyConnections()
 {
   m_connections["one"]= "a" ;
   m_connections["two"]= "b" ;
   m_connections["three"]= "c" ; 
   m_connections["four"]= "d" ; 
 }

template<class archive>
void serialize(archive& ar, const unsigned int version)
{
    using boost::serialization::make_nvp;
    ar & make_nvp("Connections", m_connections);
}

public:
 map<string,string> m_connections ;
};

int main(int argc, char* argv[])
{
  std::ofstream ofs("output.xml");
 const MyConnections conn;
 boost::archive::xml_oarchive xml(ofs);
 xml << boost::serialization::make_nvp("Connections", conn);
}

The code above is perfectly fine and is creating the xml file, i tried posting the output but failed to understand how to post an xml file

Now, i tried to change the code to cater a structure as a map's value

Issue 1

struct values
{
 std::string a;
 std::string b;
 values():a("milepost"),b("dummyval"){};
};


class MyConnections
{
 public:
 MyConnections()
 {
   m_connections["one"]= a ;
   m_connections["two"]= b ;
   m_connections["three"]= c ; 
   m_connections["four"]= d ; 
 }

template<class archive>
void serialize(archive& ar, const unsigned int version)
{
    using boost::serialization::make_nvp;
    ar & make_nvp("Connections", m_connections);
}

public:
 values a,b,c,d;
 map<string,values> m_connections ;
};

int main(int argc, char* argv[])
{
  std::ofstream ofs("output.xml");
 const MyConnections conn;
 values obj;
 std::cout<<obj.a<<obj.b<<"\n";
 boost::archive::xml_oarchive xml(ofs);
 xml << boost::serialization::make_nvp("Connections", conn);
}

Below is the error encountered

In file included from /usr/local/include/boost/serialization/map.hpp:25: /usr/local/include/boost/serialization/access.hpp:116:11: error: no member named 'serialize' in 'values' t.serialize(ar, file_version); ~ ^ /usr/local/include/boost/serialization/serialization.hpp:68:13: note: in instantiation of function template specialization 'boost::serialization::access::serialize' requested here access::serialize(ar, t, static_cast(file_version)); ^ /usr/local/include/boost/serialization/serialization.hpp:126:5: note: in instantiation of function template specialization 'boost::serialization::serialize' requested here serialize(ar, t, v); ^ /usr/local/include/boost/archive/detail/oserializer.hpp:149:27: note: in instantiation of function template specialization 'boost::serialization::serialize_adl' requested here boost::serialization::serialize_adl( ^ /usr/local/include/boost/serialization/singleton.hpp:122:47: note: in instantiation of member function 'boost::archive::detail::oserializer::save_object_data' requested here static detail::singleton_wrapper< T > t; ^ /usr/local/include/boost/serialization/singleton.hpp:135:16: note: in instantiation of member function 'boost::serialization::singleton >::get_instance' requested here return get_instance(); ^ /usr/local/include/boost/archive/detail/oserializer.hpp:258:20: note: (skipping 44 contexts in backtrace; use -ftemplate-backtrace-limit=0 to see all) ::get_const_instance() ^ /usr/local/include/boost/archive/detail/oserializer.hpp:526:12: note: in instantiation of function template specialization 'boost::archive::detail::save_non_pointer_type::invoke' requested here typex::invoke(ar, t); ^ /usr/local/include/boost/archive/detail/common_oarchive.hpp:70:18: note: in instantiation of function template specialization 'boost::archive::save' requested here archive::save(* this->This(), t); ^ /usr/local/include/boost/archive/basic_xml_oarchive.hpp:100:39: note: in instantiation of function template specialization 'boost::archive::detail::common_oarchive::save_override' requested here this->detail_common_oarchive::save_override(t.const_value()); ^ /usr/local/include/boost/archive/detail/interface_oarchive.hpp:70:23: note: in instantiation of function template specialization 'boost::archive::basic_xml_oarchive::save_override' requested here this->This()->save_override(t); ^ teee.cpp:44:9: note: in instantiation of function template specialization 'boost::archive::detail::interface_oarchive::operator<< >' requested here xml << boost::serialization::make_nvp("Connections", conn); ^

Please advice whats wrong and explain as i am new to boost aim this is my first day

Update i tried adding serialise function to structure values and it worked

struct values 
{
 std::string a;
 std::string b;
 values():a("milepost"),b("dummyval"){};
    template<class archive>

void serialize(archive& ar, const unsigned int version)
{
    using boost::serialization::make_nvp;
    ar & make_nvp("values", a);
    ar & make_nvp("values", b);

}

};

Thanks Tejas

Aucun commentaire:

Enregistrer un commentaire