Search code examples
c++boost-serialization

Error: undefined reference to `bbque::Event::Event()' on .../boost/serialization/access.hpp:132


I'm using boost v.1.55 library to serialize/deserialize but when I compile I have this error. I'm new to C++ programming and Boost libraries.

event.h

    #ifndef BBQUE_EVENT_H_
    #define BBQUE_EVENT_H_

    #include <cstdint>
    #include <string>
    #include <ctime>
    #include <boost/serialization/access.hpp>

    namespace bbque 
    {
    class Event
    {

    public:

        Event();

        Event(std::string const & module, std::string const & type, const int & value);
        ~Event();

        inline std::string GetModule() const{
            return this->module;
        }

        inline std::string GetType() const{
            return this->type;
        }

        inline std::time_t GetTimestamp() const{
            return this->timestamp;
        }

        inline int GetValue() const{
            return this->value;
        }

        inline void SetTimestamp(std::time_t timestamp) {
            this->timestamp = timestamp;
        }

        inline void setValue(int v){
            this->value = v;
        }

        inline void setType(std::string t){
            this->type = t;
        }

        inline void setModule(std::string m){
            this->module = m;
        }

    private:

        friend class boost::serialization::access;
        template<class Archive>
        void serialize(Archive & ar, const unsigned int version)
        {
            if (version == 0 || version != 0)
            {
                ar & timestamp;
                ar & module;
                ar & type;
                ar & value;
            }
        }

        std::time_t timestamp;

        std::string module;

        std::string type;

        int value;
    };
    } //namespace bbque
    #endif // BBQUE_EVENT_H

event.cpp

#include "event.h"

using namespace bbque;

Event::Event(std::string const & module, std::string const & type, const int & value):

    timestamp(0),
    module(module),
    type(type),
    value(value) {

}

Event::~Event() {

}

access.hpp (error line @new)

template<class T>
static void construct(T * t){
    // default is inplace invocation of default constructor
    // Note the :: before the placement new. Required if the
    // class doesn't have a class-specific placement new defined.
    ::new(t)T;
}

How can I fix this problem?


Solution

  • The linker error says that you are using the default constructor (and have declared it), but it is not defined anywhere. Define it (e.g. in event.cpp or inline in event.h), then everything should be fine.