
Hi All, I am working with the serialization library and I made an example class like this. class Contact{ public: Contact(){} Contact(string& name, int phonenumber,long contactdate):name_(name), pnumber_(phonenumber),contactdate_(contactdate){} const string getName() const{return name_;} const long getPhone() const{return pnumber_;} const long getDate() const{return contactdate_;} private: string name_; long pnumber_,contactdate_; friend class boost::serialization::access; template<typename Archive> void serialize(Archive& ar, const unsigned int version){ ar & boost::serialization::make_nvp("name", name_); ar & boost::serialization::make_nvp("phone", pnumber_); ar & boost::serialization::make_nvp("contactdate", contactdate_); } }; Now I would like to allocate the xml file created with the use of this class in a vector, and this is the function: void getFromFile(std::vector<Contact>& contacts,const char* file){ std::ifstream ifile(file); boost::archive::xml_iarchive ia(ifile); ia >> contacts; ifile.close(); } The compiler complains because it misses a NVP wrapper for the class. I could not find an example for that in boost. Could you help me ? Thanks in advance Simone

On 3/18/2011 9:30 AM, simone pilozzi wrote:
Now I would like to allocate the xml file created with the use of this class in a vector, and this is the function:
void
getFromFile(std::vector<Contact>& contacts,constchar* file){
std::ifstream ifile(file);
boost::archive::xml_iarchive ia(ifile);
ia >> contacts;
ifile.close();
}
You need to specify an NVP for contacts as well, like so: ia >> boost::serialization::make_nvp( "contacts", contacts );

simone pilozzi a écrit :
Hi All,
I am working with the serialization library and I made an example class like this.
class
Contact{
public:
Contact(){}
Contact(string& name, int phonenumber,long contactdate):name_(name),
pnumber_(phonenumber),contactdate_(contactdate){}
const string getName() const{return name_;}
const long getPhone() const{return pnumber_;}
const long getDate() const{return contactdate_;}
private
:
string name_;
long pnumber_,contactdate_; friend class boost::serialization::access; template<typename Archive> void serialize(Archive& ar, const unsigned int version){
ar & boost::serialization::make_nvp(
"name", name_);
ar & boost::serialization::make_nvp(
"phone", pnumber_);
ar & boost::serialization::make_nvp(
"contactdate", contactdate_);
}
};
Now I would like to allocate the xml file created with the use of this class in a vector, and this is the function:
void
getFromFile(std::vector<Contact>& contacts,const char* file){
std::ifstream ifile(file);
boost::archive::xml_iarchive ia(ifile);
ia >> contacts;
ifile.close();
}
The compiler complains because it misses a NVP wrapper for the class. I could not find an example for that in boost.
Could you help me ?
Thanks in advance
Simone
Hi simone I guess you should use : ia >> boost::serialization::make_nvp("contacts", contacts); in place of what you propose (which is correct for text_archive by the way). AFAIU, the NVP wrapper is needed at all levels of your (de)serialization code, not only within the 'serialize' methods for class attributes. have a look on the XML archives you can generate with such method (using some basic text editor), you will understand how Boost/serialization XML archives system generates XML tags and why NVP wrappers are mandatory here. regards frc -- François Mauger Groupe "Interactions Fondamentales et Nature du Neutrino" NEMO-3/SuperNEMO Collaboration LPC Caen-CNRS/IN2P3-UCBN-ENSICAEN Département de Physique -- Université de Caen Basse-Normandie Adresse/address: Laboratoire de Physique Corpusculaire de Caen (UMR 6534) ENSICAEN 6, Boulevard du Marechal Juin 14050 CAEN Cedex FRANCE Courriel/e-mail: mauger@lpccaen.in2p3.fr Tél./phone: 02 31 45 25 12 / (+33) 2 31 45 25 12 Fax: 02 31 45 25 49 / (+33) 2 31 45 25 49
participants (3)
-
François Mauger
-
Kenny Riddile
-
simone pilozzi