我正在使用boost序列化來保存和加載數據。我的目標是存儲包含對象的矢量。他們的類型是派生類。錯誤LNK2005(已在對象中定義)使用派生類的boost序列化
鏈接器告訴我,有一些東西已經在另一個類的obj文件中定義了(這與我的序列化無關)。
有人能告訴我我的代碼有什麼問題嗎?我錯過了什麼嗎?
我的序列化派生類的代碼是否正確?
這是輸出:
錯誤1個錯誤LNK2005:「市民:靜態結構的boost ::檔案::詳細:: extra_detail :: guid_initializer常量&常量的boost ::存檔:: detail :: extra_detail :: init_guid :: g「(?g @?$ init_guid @ VCYesNoElement @@@ extra_detail @ detail @ archive @ boost @@ 2ABU?$ guid_initializer @ VCYesNoElement @@@ 2345 @ B)already defined in ElementFactory.obj
錯誤2錯誤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 @ B)在ElementFactory.obj
錯誤3錯誤LNK2005已經定義:「市民:靜態結構的boost ::檔案::詳細:: extra_detail :: guid_initializer常量& const boost :: archive :: detail :: extra_detail :: init_guid :: g「(?g @?$ init_guid @ VCYesNoElement @@@ extra_detail @ detail @ archive @ boost @@ 2ABU?$ guid_initializer @ VCYesNoElement @@@ 2345 @ B)已經在ElementFactory.obj中定義了
錯誤4錯誤LNK2005:「public:static struct boost :: archive :: detail :: extra_detail :: guid_initializer con 「stconst boost :: archive :: detail :: extra_detail :: init_guid :: g」(?g @?$ init_guid @ VCYesNoElement @@@ extra_detail @ detail @ archive @ boost @@ 2ABU?$ guid_initializer @ VCYesNoElement @@@ 2345 @ B)已經在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 @ B)已在ElementFactory中定義。物鏡
這是基類(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__
可能是因爲'BOOST_CLASS_EXPORT(CYesNoElement)'應該在'.cpp'中。 – Ylisar
哇,我不會認爲這可能是那麼簡單...非常感謝,歡迎您將它作爲答案發布,以便我可以將此問題標記爲已完成。 – Exa