none
使用Boost Serialization遇到奇怪的 LNK2019: 无法解析的外部符号 的错误 RRS feed

  • 常规讨论

  • 我的环境 :VS2008,Boost 1.43,Vista

     

    目的 :使用Boost Serialization序列化类MyBus

    类MyBus中有2个成员变量需要序列化,分别是CString m_csBusName;和vector<MyBusStop> m_vecBusStops;

    类MyBusStop中有2个成员变量需要序列化,分别是  string m_csStopName;  int m_nStopID;

    由于Boost Serialization不支持MFC的类型CString,使用如下的方式处理

    这也是Config_Boost_Serialization.cpp的代码

     

    void
     serialize(boost::archive::xml_oarchive&ar,CString& str,const
     unsigned
     int
     var){
    	string str_((LPSTR)(LPCTSTR)(str));
    	//ar<<BOOST_SERIALIZATION_NVP(str_);
    
    	ar<<boost::serialization::make_nvp("CString"
    ,str_);
    }
    void
     serialize(boost::archive::xml_iarchive&ar,CString& str,const
     unsigned
     int
     var)
    {
    	string str_;
    	ar>>boost::serialization::make_nvp("CString"
    ,str_);
    	str.Format(_T("%s"
    ),str_.c_str());
    }
    
    

     

     

    出现的问题:

    错误    2    error LNK2019: 无法解析的外部符号 "public: void __thiscall MyBusStop::serialize<class boost::archive::xml_iarchive>(class boost::archive::xml_iarchive &,unsigned int)" (??$serialize@Vxml_iarchive@archive@boost@@@MyBusStop@@QAEXAAVxml_iarchive@archive@boost@@I@Z),该符号在函数 "public: static void __cdecl boost::serialization::access::serialize<class boost::archive::xml_iarchive,class MyBusStop>(class boost::archive::xml_iarchive &,class MyBusStop &,unsigned int)" (??$serialize@Vxml_iarchive@archive@boost@@VMyBusStop@@@access@serialization@boost@@SAXAAVxml_iarchive@archive@2@AAVMyBusStop@@I@Z) 中被引用    MyBus.obj


    错误    1    error LNK2019: 无法解析的外部符号 "public: void __thiscall MyBusStop::serialize<class boost::archive::xml_oarchive>(class boost::archive::xml_oarchive &,unsigned int)" (??$serialize@Vxml_oarchive@archive@boost@@@MyBusStop@@QAEXAAVxml_oarchive@archive@boost@@I@Z),该符号在函数 "public: static void __cdecl boost::serialization::access::serialize<class boost::archive::xml_oarchive,class MyBusStop>(class boost::archive::xml_oarchive &,class MyBusStop &,unsigned int)" (??$serialize@Vxml_oarchive@archive@boost@@VMyBusStop@@@access@serialization@boost@@SAXAAVxml_oarchive@archive@2@AAVMyBusStop@@I@Z) 中被引用    MyBus.obj


    错误    3    fatal error LNK1120: 2 个无法解析的外部命令    C:\Users\Edwin\Documents\Visual Studio 2008\Projects\Boost_Serialization_Cstring\Debug\Boost_Serialization_Cstring.exe

     

    我的代码:

    MyBus.h

    #pragma once
    
    #include "MyBusStop.h"
    #include <boost/archive/xml_iarchive.hpp>
    #include <boost/archive/xml_oarchive.hpp>
    
    #include <vector>
    using std::vector;
    
    
    
    class MyBus
    {
    public:
    	MyBus(void);
    	~MyBus(void);
    
    	CString m_csBusName;
    	vector<MyBusStop> m_vecBusStops;
    
    
    	//Boost Serialize
    	friend class boost::serialization::access;
    	
    	//template<class Archive>
    	//void serialize(Archive & ar, const unsigned int version);
    
    	template<class Archive>
    	void save(Archive & ar, const unsigned int version) const;
    
    	template<class Archive>
    	void load(Archive & ar, const unsigned int version);
    
    	BOOST_SERIALIZATION_SPLIT_MEMBER()
    };
    

     

    MyBus.cpp

     

    #include "StdAfx.h"
    
    #include "MyBus.h"
    
    #include <fstream>
    #include <boost/serialization/vector.hpp>
    #include "Config_Boost_Serialization.h"
    
    
    using
     namespace
     std;
    
    
    MyBus::MyBus(void
    )
    :m_csBusName("931路"
    )
    {
    	MyBusStop aBusStop;
    	aBusStop.m_csStopName = "共和新村"
    ;
    	aBusStop.m_nStopID = 1;
    	m_vecBusStops.push_back(aBusStop);
    
    	MyBusStop aBusStop2;
    	aBusStop2.m_csStopName = "十六铺"
    ;
    	aBusStop2.m_nStopID = 2;
    	m_vecBusStops.push_back(aBusStop2);
    
    
    	try
    
    	{	
    		ifstream is("test.xml"
    );
    		boost::archive::xml_iarchive ar(is);
    		ar >> boost::serialization::make_nvp("MyBus"
    ,*this
    );
    	}
    	catch
    (...)
    	{
    		ofstream os("test.xml"
    );
    		boost::archive::xml_oarchive ar(os);
    		ar << boost::serialization::make_nvp("MyBus"
    ,*this
    );
    	}
    
    }
    
    MyBus::~MyBus(void
    )
    {
    }
    
    template
    <class
     Archive>
    void
     MyBus::save(Archive & ar, const
     unsigned
     int
     version) const
    
    {
    	ar & BOOST_SERIALIZATION_NVP(m_csBusName)
    	 & BOOST_SERIALIZATION_NVP(m_vecBusStops);
    }
    
    
    template
    <class
     Archive>
    void
     MyBus::load(Archive & ar, const
     unsigned
     int
     version)
    {
    	ar & BOOST_SERIALIZATION_NVP(m_csBusName)
    	 & BOOST_SERIALIZATION_NVP(m_vecBusStops);
    }
    

     

    MyBusStop.h

     

    #pragma once
    
    #include <boost/archive/xml_iarchive.hpp>
    #include <boost/archive/xml_oarchive.hpp>
    #include <string>
    
    using
     namespace
     std;
    
    class
     MyBusStop
    {
    public
    :
    	MyBusStop(void
    );
    	~MyBusStop(void
    );
    
    	string m_csStopName;
    	int
     m_nStopID;
    
    
    	//Boost Serialize
    
    	friend
     class
     boost::serialization::access;
    	
    	//template<class Archive>
    
    	//void serialize(Archive & ar, const unsigned int version);
    
    
    	template
    <class
     Archive>
    	void
     save(Archive & ar, const
     unsigned
     int
     version) const
    ;
    
    	template
    <class
     Archive>
    	void
     load(Archive & ar, const
     unsigned
     int
     version);
    
    	BOOST_SERIALIZATION_SPLIT_MEMBER()
    
    };
    
    

     

    MyBusStop.cpp

     

    #include "StdAfx.h"
    
    #include "MyBusStop.h"
    
    #include "Config_Boost_Serialization.h"
    
    
    
    MyBusStop::MyBusStop(void
    )
    {
    }
    
    MyBusStop::~MyBusStop(void
    )
    {
    }
    
    
    template
    <class
     Archive>
    void
     MyBusStop::save(Archive & ar, const
     unsigned
     int
     version) const
    
    {
    	ar & BOOST_SERIALIZATION_NVP(m_csStopName)
    	 & BOOST_SERIALIZATION_NVP(m_nStopID);
    }
    
    
    template
    <class
     Archive>
    void
     MyBusStop::load(Archive & ar, const
     unsigned
     int
     version)
    {
    	ar & BOOST_SERIALIZATION_NVP(m_csStopName)
    	 & BOOST_SERIALIZATION_NVP(m_nStopID);
    }
    
    
    //template<class Archive>
    
    //void MyBusStop::serialize(Archive & ar, const unsigned int version)
    
    //{
    
    //	ar & BOOST_SERIALIZATION_NVP(m_csStopName)
    
    //	 & BOOST_SERIALIZATION_NVP(m_nStopID);
    
    //}
    
    //
    
    
    

     

    2010年7月25日 5:28

全部回复