0

I'm trying to serialize my abstract class according to those questions:

My neuron.h looks like this:

class Neuron {

public:

    struct access;

    API virtual ~Neuron();

    API virtual double activate( double x, double b ) = 0;

};

I have to keep all the Boost related members in neuron.cpp to prevent including Boost headers when using neuron.h in some other codes.

My neuron.cpp looks like this:

#include "Neuron.h"

#include <boost/archive/text_oarchive.hpp>
#include <boost/archive/text_iarchive.hpp>

BOOST_SERIALIZATION_ASSUME_ABSTRACT(Neuron);

struct Neuron :: access {
    template <class Archive>
    static void serialize(Archive &ar, Neuron& n, const unsigned int version) {}
};

namespace boost {
    namespace serialization {

        template<class Archive>
        void serialize(Archive & ar, Neuron& n, const unsigned int version)
        {
            Neuron::access::serialize(ar, n, version);
        }

    } // namespace serialization
} // namespace boost

Neuron::~Neuron() {

}

The problem is, that when I'm using its inherited classes elsewhere, I'm getting the error

***/boost/boost/serialization/access.hpp:116:11: error: ‘class Neuron’ has no member named ‘serialize’

What am I doing wrong here?

Eenoku
  • 2,741
  • 4
  • 32
  • 64
  • Well, Neuron::access::serialize(ar, n, version); calls the serialization of neuron which is not implemented. you have to specify exaclty how n is to serialized. –  Sep 14 '18 at 12:23
  • @Pi So, the `serialize` function is implemented in the `access` struct. I was trying to follow [the example](http://coliru.stacked-crooked.com/a/5d76b1aa22076a77) given in the first listed question. – Eenoku Sep 14 '18 at 12:27
  • Yeah, but in the example it says ``ar & BOOST_SERIALIZATION_NVP(a.m_id);` which is concrete way of serializing the member data. You do not have an equivallent –  Sep 14 '18 at 12:29
  • @Pi Yes, but I have no members to serialize in this abstract class (it's currently just the set of methods) - I'm implementing it only so the inherited classes can call it. – Eenoku Sep 14 '18 at 12:30
  • @Pi Or do you think, that there is some special way to make an empty serialization function? – Eenoku Sep 14 '18 at 12:31

1 Answers1

0

I think the key here is "when I'm using its inherited classes elsewhere". Correct me (and your question, please) if I'm wrong, but this suggests that you are getting the compile error while compiling a source file other than neuron.cpp.

This makes sense, given what the compiler has to work with. You might have noticed that changes to one source file tend to not require re-compiling other source files. So adding something -- like an overload of serialize() -- to neuron.cpp does not change how other translation units are compiled. (It can change how everything is linked together in the end, but we're not there yet.) If another translation unit tries to serialize Neuron, the stuff in neuron.cpp does not matter. The compiler is not aware of an appropriate overload of serialize(), so serializing Neuron in another source file results in intrusive serialization. That is, the compiler will look for a member function of Neuron called serialize().

In order for your overload of serialize() to affect how other translation units are compiled, it needs to be declared in a header file.

Since you cannot put Boost stuff in neuron.h, you might have to create a new header file, say neuron_boost.h. This file would #include "neuron.h" then provide the declarations needed for Boost serialization. Source files that serialize descendants of Neuron would include neuron_boost.h while other source files could continue to include the original neuron.h.

JaMiT
  • 14,422
  • 4
  • 15
  • 31
  • I've managed to get rid of the mentioned error without having to move `serialize` function to another header. The funny thing is, that the same error appeared in a different part of the code. I've tried to describe all the important details in my answer in the `EDIT` section. – Eenoku Sep 16 '18 at 19:57