错误:在 .../boost/serialization/access.hpp:132 上未定义对"bbque::Event::Event()"的引用

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

本文关键字:Event bbque 引用 serialization boost access hpp 错误 未定义      更新时间:2023-10-16

我使用boost v.1.55库来序列化/反序列化,但是当我编译时,我有这个错误。我是c++编程和Boost库的新手。

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(错误行@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;
}

如何解决这个问题?

链接器错误表明您正在使用默认构造函数(并且已经声明了它),但是没有在任何地方定义它。定义它(例如在event.cpp或在event.h内嵌),那么一切都应该没问题。