Ошибка LNK2005 (уже определена в объекте) при использовании расширенной сериализации с производными классами

Я использую сериализацию Boost для сохранения и загрузки данных. Моя цель — хранить вектор, содержащий объекты. Их тип является производным классом.

Компоновщик говорит мне, что есть некоторые вещи, которые уже определены в obj-файле другого класса (который не имеет никакого отношения к моей сериализации).

Может кто-нибудь сказать мне, что не так с моим кодом? Я что-то пропустил?
Правильный ли мой код для сериализации производных классов?


Это выход:

Ошибка 1, ошибка LNK2005: «public: static struct boost :: archive :: detail :: extra_detail :: guid_initializer const & const boost :: archive :: detail :: extra_detail :: init_guid :: g «(? g @? $ init_guid @ VCYesNoElement @@@ extra_detail @ detail @ archive @ boost @@ 2ABU? $ guid_initializer @ VCYesNoElement @@@ 2345 @ Б) уже определено в ElementFactory.obj

Ошибка 2, ошибка LNK2005: «public: статическая структура boost :: archive :: detail :: extra_detail :: guid_initializer const & const boost :: archive :: detail :: extra_detail :: init_guid :: g «(? g @? $ init_guid @ VCYesNoElement @@@ extra_detail @ detail @ archive @ boost @@ 2ABU? $ guid_initializer @ VCYesNoElement @@@ 2345 @ Б) уже определено в ElementFactory.obj

Ошибка 3, ошибка LNK2005: «public: static struct boost :: archive :: detail :: extra_detail :: guid_initializer const & const boost :: archive :: detail :: extra_detail :: init_guid :: g «(? g @? $ init_guid @ VCYesNoElement @@@ extra_detail @ detail @ archive @ boost @@ 2ABU? $ guid_initializer @ VCYesNoElement @@@ 2345 @ Б) уже определено в ElementFactory.obj

Ошибка 4, ошибка LNK2005: «public: статическая структура boost :: archive :: detail :: extra_detail :: guid_initializer const & const boost :: archive :: detail :: extra_detail :: init_guid :: g «(? g @? $ init_guid @ VCYesNoElement @@@ extra_detail @ detail @ archive @ boost @@ 2ABU? $ guid_initializer @ VCYesNoElement @@@ 2345 @ Б) уже определено в ElementFactory.obj

Ошибка 5, ошибка LNK2005: «public: static struct boost :: archive :: detail :: extra_detail :: guid_initializer const & const boost :: archive :: detail :: extra_detail :: init_guid :: g «(? g @? $ init_guid @ VCYesNoElement @@@ extra_detail @ detail @ archive @ boost @@ 2ABU? $ guid_initializer @ VCYesNoElement @@@ 2345 @ Б) уже определено в ElementFactory.obj


Это базовый класс (IElement):

#ifndef __ELEMENT_H__
#define __ELEMENT_H__

#include <string>
#include <vector>
#include <cassert>

#include <boost\serialization\base_object.hpp>
#include <boost\serialization\export.hpp>
#include <boost\archive\binary_iarchive.hpp>
#include <boost\archive\binary_oarchive.hpp>

typedef enum eElementType
{
eMultipleChoice,
eYesNo,
eKeyword,
eText,
eCloze
} eType;

class IElement
{

public:
virtual ~IElement();

void SetFrontText( const std::string& question );
std::string GetFrontText();
virtual eType GetType() = 0;

int GetStatus();
void StatusUp();
void StatusDown();
void Reset();

template<class Archive>
void serialize(Archive & ar, const unsigned int version)
{
ar & boost::serialization::make_nvp( "Status", m_status );
ar & boost::serialization::make_nvp( "FrontText", m_frontText );
}

protected:
bool VectorContainsString( std::vector<std::string>* _vec, const std::string& _str );
bool RemoveStringFromVector( std::vector<std::string>* _vec, const std::string& _str );

std::string m_frontText;

int m_status;
};
BOOST_SERIALIZATION_ASSUME_ABSTRACT( IElement )

#endif // __ELEMENT_H__

Это производный класс (CYesNoElement):

#ifndef __YES_NO_ELEMENT_H__
#define __YES_NO_ELEMENT_H__

#include "Element.h"
class CYesNoElement : public IElement
{
public:
CYesNoElement();
virtual ~CYesNoElement();

bool GetSolution();
void SetSolution( bool solution );

bool check( bool given_answer );

// IElement
virtual eType GetType();

template<class Archive>
void serialize(Archive & ar, unsigned int file_version)
{
BOOST_SERIALIZATION_BASE_OBJECT_NVP( IElement );
ar & boost::serialization::make_nvp( "Solution", m_solution );
}

private:
bool m_solution;
};
BOOST_CLASS_EXPORT(CYesNoElement)

#endif // __YES_NO_ELEMENT_H__

Это класс CElementFactory где ошибки компоновщика намекают на:

#ifndef __ELEMENTFACTORY_H__
#define __ELEMENTFACTORY_H__

#include "ClozeElement.h"#include "KeywordElement.h"#include "MultipleChoiceElement.h"#include "TextElement.h"#include "YesNoElement.h"
class CElementFactory
{
public:
CElementFactory(void);
virtual ~CElementFactory(void);

IElement* CreateElement( eType type );

private:
CClozeElement* CreateClozeElement();
CKeywordElement* CreateKeywordElement();
CMultipleChoiceElement* CreateMultipleChoiceElement();
CTextElement* CreateTextElement();
CYesNoElement* CreateYesNoElement();
};

#endif // __ELEMENTFACTORY_H__

1

Решение

Положил BOOST_CLASS_EXPORT(CYesNoElement) в .cpp,

2

Другие решения

Других решений пока нет …