I am experimenting with the boost serialization library and I've gotten most of it working. The only problem is when i try to serialize an object that has separate .h and .cpp files. When i compile using this command:
g++ boostSerialize.cpp Class.cpp -lboost_serialization
i get this error:
/tmp/cc8kbW6J.o: In function `void boost::serialization::access::serialize<boost::archive::text_oarchive, Class>(boost::archive::text_oarchive&, Class&, unsigned int)':
boostSerialize.cpp:(.text._ZN5boost13serialization6access9serializeINS_7archive13text_oarchiveE5ClassEEvRT_RT0_j[void boost::serialization::access::serialize<boost::archive::text_oarchive, Class>(boost::archive::text_oarchive&, Class&, unsigned int)]+0x25): undefined reference to `void Class::serialize<boost::archive::text_oarchive>(boost::archive::text_oarchive&, unsigned int)'
this is what is in my .h:
#ifndef CLASS_H
#define CLASS_H
#include <iostream>
#include <string>
#include <boost/serialization/access.hpp>
using namespace std;
class Class{
friend class boost::serialization::access;
int a,b,c;
string stringy;
template<class Archive>
void serialize(Archive &ar, const unsigned int);
public:
Class(int ab, int bb, int cb);
};
#endif
and my .cpp:
#include <iostream>
#include "Class.h"
using namespace std;
Class::Class(int ab, int bb, int cb){
a = ab;
b = bb;
c = cb;
stringy = "Text";
}
template<class Archive>
void Class::serialize(Archive &ar, const unsigned int){
ar & a & b & c & stringy;
}
I tried instead putting everything only into a .cpp and including that and it worked fine, so i know that it is something to do with the .h inclusion. For some reason it is not finding the serialize function? I guess i could just use a .cpp instead of both but i really like organization and i would like to use this for a big project. Any ideas? Thanks in advance.
Your problem is not with Boost.Serialization (as such), but that you are trying to separately compile a function template.
Class::serialize
is a function template, which means that it gets instantiated on the basis of the types of the template parameters that get passed to is. When compiling Class.cpp
, the compiler does not know what types Class::serialize
will be instantiated with, and so it cannot generate the code.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With