Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/134.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 使用派生类的boost序列化时出现错误LNK2005(已在对象中定义)_C++_Boost_Boost Serialization - Fatal编程技术网

C++ 使用派生类的boost序列化时出现错误LNK2005(已在对象中定义)

C++ 使用派生类的boost序列化时出现错误LNK2005(已在对象中定义),c++,boost,boost-serialization,C++,Boost,Boost Serialization,我正在使用boost序列化来保存和加载数据。我的目标是存储包含对象的向量。它们的类型是派生类 链接器告诉我,在另一个类的obj文件中已经定义了一些内容(这与我的序列化无关) 有人能告诉我我的代码有什么问题吗?我错过什么了吗? 我序列化派生类的代码正确吗 这是输出: 错误1错误LNK2005:“public:static struct boost::archive::detail::extra_detail::guid_初始值设定项const&const boost::archive::deta

我正在使用boost序列化来保存和加载数据。我的目标是存储包含对象的向量。它们的类型是派生类

链接器告诉我,在另一个类的obj文件中已经定义了一些内容(这与我的序列化无关)

有人能告诉我我的代码有什么问题吗?我错过什么了吗?
我序列化派生类的代码正确吗


这是输出

错误1错误LNK2005:“public:static struct boost::archive::detail::extra_detail::guid_初始值设定项const&const boost::archive::detail::extra_detail::init_guid::g”(?g@?$init)_guid@VCYesNoElement@@@额外的_detail@detail@archive@boost@@2ABU?$guid_initializer@VCYesNoElement@@@2345@B)已在ElementFactory.obj中定义

错误2错误LNK2005:“public:static struct boost::archive::detail::extra_detail::guid_初始值设定项const&const boost::archive::detail::extra_detail::init_guid::g”(?g@?$init)_guid@VCYesNoElement@@@额外的_detail@detail@archive@boost@@2ABU?$guid_initializer@VCYesNoElement@@@2345@B)已在ElementFactory.obj中定义

错误3错误LNK2005:“public:static struct boost::archive::detail::extra_detail::guid_初始值设定项const&const boost::archive::detail::extra_detail::init_guid::g”(?g@?$init)_guid@VCYesNoElement@@@额外的_detail@detail@archive@boost@@2ABU?$guid_initializer@VCYesNoElement@@@2345@B)已在ElementFactory.obj中定义

错误4错误LNK2005:“public:static struct boost::archive::detail::extra_detail::guid_初始值设定项const&const boost::archive::detail::extra_detail::init_guid::g”(?g@?$init)_guid@VCYesNoElement@@@额外的_detail@detail@archive@boost@@2ABU?$guid_initializer@VCYesNoElement@@@2345@B)已在ElementFactory.obj中定义

错误5错误LNK2005:“public:static struct boost::archive::detail::extra_detail::guid_初始值设定项const&const boost::archive::detail::extra_detail::init_guid::g”(?g@?$init)_guid@VCYesNoElement@@@额外的_detail@detail@archive@boost@@2ABU?$guid_initializer@VCYesNoElement@@@2345@B)已在ElementFactory.obj中定义


这是基类(IEElement):


BOOST\u CLASS\u EXPORT(CYesNoElement)
放在
.cpp
中,可能是因为
BOOST\u CLASS\u EXPORT(CYesNoElement)
应该放在
.cpp
中。哇,我没想到会这么容易。。。非常感谢,欢迎您将其作为答案发布,以便我可以将此问题标记为已完成。
#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__
#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__
#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__