5

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.

1

1 Answer 1

8

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.

4
  • So how do I tell the compiler what types it will be instantiated with? Or can i not? Commented Mar 20, 2012 at 22:41
  • @adamk33n3r: Good question! You can explicitly instansiate it in the .cpp file with template void Class::serialize<boost::archive::text_oarchive>(boost::archive::text_oarchive &ar, const unsigned int);.
    – Mankarse
    Commented Mar 20, 2012 at 22:48
  • Thanks so much! You are a brain meltdown saver! I've been trying to figure this one out for a while. Commented Mar 20, 2012 at 22:56
  • 3
    Better (?) solution: Move the implementation of your serialize method into the header. This is the typical approach for ALL templates - their full implementation has to go in the header.
    – aldo
    Commented May 19, 2012 at 15:45

Not the answer you're looking for? Browse other questions tagged or ask your own question.