2012-05-03 64 views
3

我想找到一個很好的例子,說明如何使用這些boost的序列化的東西的二進制寬字符版本。我拼湊了一些代碼來試着讓它工作,但不幸的是,當我試圖編譯它時,我遇到了鏈接器錯誤。boost/archive/binary_woarchive.hpp和/或boost/archive/binary_wiarchive.hpp的序列化示例?

這裏是我的代碼,如果我做任何事情顯然是錯誤的:

#include <cstdlib> 
    #include <iostream> 
    #include <fstream> 
    #include <string> 
    #include <vector> 

    #include <boost/archive/binary_woarchive.hpp> 
    #include <boost/archive/binary_wiarchive.hpp> 

    class testClass 
    { 
    public: 
     testClass() 
     { 
     } 
     testClass(const int intInput, const std::wstring stringInput, const float floatInput01, const float floatInput02) 
     { 
      ourString = stringInput; 
      testInt = intInput; 
      testFloat01 = floatInput01; 
      testFloat02 = floatInput02; 
     } 
     ~testClass() 
     {} 

     int testInt; 
     std::wstring ourString; 
     float testFloat01; 
     float testFloat02; 

     int ReturnTestInt() 
     { 
      return testInt; 
     } 

     float RandomStuff() 
     { 
      float alpha01 = 26.9; 
      alpha01 /= 2; 
      return alpha01; 
     } 

    private: 
     friend class boost::serialization::access; 
     template<class Archive> 
     void serialize(Archive &ar, const unsigned int version) 
     { 
      ar &testInt; 
      ar &ourString; 
      ar &testFloat01; 
      ar &testFloat02; 
     } 
    }; 

    int main() 
    { 
     std::vector<const testClass> objectStorage; 


     objectStorage.push_back(testClass(1, L"test\0", 9.14f, 6.662f)); 
     objectStorage.push_back(testClass(2, L"temp\0", 0.29f, 3.331f)); 
     objectStorage.push_back(testClass(3, L"then\0", 2.47f, 8.888f)); 


     testClass testReceivedObject; 
     std::ifstream::pos_type size; 

     std::wofstream myFile; 
     boost::archive::binary_woarchive outputArchive(myFile); 
     myFile.open("Example.dat", std::ios::out | std::ios::binary); 

     if(myFile.is_open() && myFile.good()) 
     { 
      std::cout<<"File opening successfully completed."<<std::endl; 
      unsigned int storageSize = objectStorage.size(); 

      myFile.write(reinterpret_cast<wchar_t*>(&storageSize), (sizeof(unsigned int))); 

      for(int i = 0; i < objectStorage.size(); i++) 
      { 
       outputArchive<<objectStorage[i]; 
      } 
      /* 
      myFile.write(reinterpret_cast<char*>(&objectStorage[0]), (sizeof(testClass))); 
      myFile.write(reinterpret_cast<char*>(&objectStorage[1]), (sizeof(testClass))); 
      myFile.write(reinterpret_cast<char*>(&objectStorage[2]), (sizeof(testClass))); 
     */ 
     } 
     else 
     { 
      std::cout<<"File opening NOT successfully completed."<<std::endl; 
     } 
     myFile.close(); 

     std::wifstream myFileInput; 
     boost::archive::binary_wiarchive inputArchive(myFileInput); 
     myFileInput.open("Example.dat", std::ios::in | std::ios::binary | std::ios::ate); 

     if(myFileInput.is_open() && myFileInput.good()) 
     { 
      std::cout<<"File opening successfully completed. Again."<<std::endl; 

      std::cout<<"READ:"<<std::endl; 
      size = myFileInput.tellg(); 
      unsigned int numberOfObjects = 0; 
      myFileInput.seekg(0, std::ios::beg);    
myFileInput.read(reinterpret_cast<wchar_t *>(&numberOfObjects), sizeof(unsigned int)); 
      for(int i = 0; i < numberOfObjects; i++) 
      { 
       objectStorage.resize(objectStorage.size()+1); 
       inputArchive>>objectStorage[i]; 
       //myFileInput.read(reinterpret_cast<wchar_t *>(&objectStorage[i])/*(&testReceivedObject)*/, sizeof(testClass)); 
      } 
      std::cout<<std::endl<<"END."<<std::endl; 
     } 
     else 
     { 
      std::cout<<"Something has gone disasterously wrong."<<std::endl; 
     } 
     myFileInput.close(); 

     for(int i = 0; i < objectStorage.size(); i++) 
     { 
      std::wcout<<objectStorage[i].ourString<<std::endl; 
     } 
     return 0; 
    } 

這裏是錯誤的:在理解什麼是錯的,以及如何解決這將是

1>main.obj : error LNK2019: unresolved external symbol "protected: __thiscall boost::archive::basic_binary_oprimitive<class boost::archive::binary_woarchive,wchar_t,struct std::char_traits<wchar_t> >::~basic_binary_oprimitive<class boost::archive::binary_woarchive,wchar_t,struct std::char_traits<wchar_t> >(void)" ([email protected][email protected]@[email protected]@[email protected][email protected]@@@[email protected]@@[email protected]) referenced in function "public: __thiscall boost::archive::binary_oarchive_impl<class boost::archive::binary_woarchive,wchar_t,struct std::char_traits<wchar_t> >::~binary_oarchive_impl<class boost::archive::binary_woarchive,wchar_t,struct std::char_traits<wchar_t> >(void)" ([email protected][email protected]@[email protected]@[email protected][email protected]@@@[email protected]@@[email protected]) 
1>main.obj : error LNK2019: unresolved external symbol "protected: __thiscall boost::archive::basic_binary_iprimitive<class boost::archive::binary_wiarchive,wchar_t,struct std::char_traits<wchar_t> >::~basic_binary_iprimitive<class boost::archive::binary_wiarchive,wchar_t,struct std::char_traits<wchar_t> >(void)" ([email protected][email protected]@[email protected]@[email protected][email protected]@@@[email protected]@@[email protected]) referenced in function "public: __thiscall boost::archive::binary_iarchive_impl<class boost::archive::binary_wiarchive,wchar_t,struct std::char_traits<wchar_t> >::~binary_iarchive_impl<class boost::archive::binary_wiarchive,wchar_t,struct std::char_traits<wchar_t> >(void)" ([email protected][email protected]@[email protected]@[email protected][email protected]@@@[email protected]@@[email protected]) 
1>main.obj : error LNK2019: unresolved external symbol "protected: __thiscall boost::archive::basic_binary_oprimitive<class boost::archive::binary_woarchive,wchar_t,struct std::char_traits<wchar_t> >::basic_binary_oprimitive<class boost::archive::binary_woarchive,wchar_t,struct std::char_traits<wchar_t> >(class std::basic_streambuf<wchar_t,struct std::char_traits<wchar_t> > &,bool)" ([email protected][email protected]@[email protected]@[email protected][email protected]@@@[email protected]@@[email protected][email protected][email protected][email protected]@@@[email protected]@[email protected]) referenced in function "protected: __thiscall boost::archive::binary_oarchive_impl<class boost::archive::binary_woarchive,wchar_t,struct std::char_traits<wchar_t> >::binary_oarchive_impl<class boost::archive::binary_woarchive,wchar_t,struct std::char_traits<wchar_t> >(class std::basic_ostream<wchar_t,struct std::char_traits<wchar_t> > &,unsigned int)" ([email protected][email protected]@[email protected]@[email protected][email protected]@@@[email protected]@@[email protected][email protected][email protected][email protected]@@@[email protected]@[email protected]) 
1>main.obj : error LNK2019: unresolved external symbol "protected: __thiscall boost::archive::basic_binary_iprimitive<class boost::archive::binary_wiarchive,wchar_t,struct std::char_traits<wchar_t> >::basic_binary_iprimitive<class boost::archive::binary_wiarchive,wchar_t,struct std::char_traits<wchar_t> >(class std::basic_streambuf<wchar_t,struct std::char_traits<wchar_t> > &,bool)" ([email protected][email protected]@[email protected]@[email protected][email protected]@@@[email protected]@@[email protected][email protected][email protected][email protected]@@@[email protected]@[email protected]) referenced in function "protected: __thiscall boost::archive::binary_iarchive_impl<class boost::archive::binary_wiarchive,wchar_t,struct std::char_traits<wchar_t> >::binary_iarchive_impl<class boost::archive::binary_wiarchive,wchar_t,struct std::char_traits<wchar_t> >(class std::basic_istream<wchar_t,struct std::char_traits<wchar_t> > &,unsigned int)" ([email protected][email protected]@[email protected]@[email protected][email protected]@@@[email protected]@@[email protected][email protected][email protected][email protected]@@@[email protected]@[email protected]) 
1>main.obj : error LNK2019: unresolved external symbol "protected: void __thiscall boost::archive::basic_binary_oprimitive<class boost::archive::binary_woarchive,wchar_t,struct std::char_traits<wchar_t> >::init(void)" ([email protected][email protected][email protected]@[email protected]@[email protected][email protected]@@@[email protected]@@IAEXXZ) referenced in function "protected: void __thiscall boost::archive::binary_oarchive_impl<class boost::archive::binary_woarchive,wchar_t,struct std::char_traits<wchar_t> >::init(unsigned int)" ([email protected][email protected][email protected]@[email protected]@[email protected][email protected]@@@[email protected]@@[email protected]) 
1>main.obj : error LNK2019: unresolved external symbol "protected: void __thiscall boost::archive::basic_binary_oarchive<class boost::archive::binary_woarchive>::init(void)" ([email protected][email protected][email protected]@[email protected]@@[email protected]@@IAEXXZ) referenced in function "protected: void __thiscall boost::archive::binary_oarchive_impl<class boost::archive::binary_woarchive,wchar_t,struct std::char_traits<wchar_t> >::init(unsigned int)" ([email protected][email protected][email protected]@[email protected]@[email protected][email protected]@@@[email protected]@@[email protected]) 
1>main.obj : error LNK2019: unresolved external symbol "protected: void __thiscall boost::archive::basic_binary_iprimitive<class boost::archive::binary_wiarchive,wchar_t,struct std::char_traits<wchar_t> >::init(void)" ([email protected][email protected][email protected]@[email protected]@[email protected][email protected]@@@[email protected]@@IAEXXZ) referenced in function "protected: void __thiscall boost::archive::binary_iarchive_impl<class boost::archive::binary_wiarchive,wchar_t,struct std::char_traits<wchar_t> >::init(unsigned int)" ([email protected][email protected][email protected]@[email protected]@[email protected][email protected]@@@[email protected]@@[email protected]) 
1>main.obj : error LNK2019: unresolved external symbol "protected: void __thiscall boost::archive::basic_binary_iarchive<class boost::archive::binary_wiarchive>::init(void)" ([email protected][email protected][email protected]@[email protected]@@[email protected]@@IAEXXZ) referenced in function "protected: void __thiscall boost::archive::binary_iarchive_impl<class boost::archive::binary_wiarchive,wchar_t,struct std::char_traits<wchar_t> >::init(unsigned int)" ([email protected][email protected][email protected]@[email protected]@[email protected][email protected]@@@[email protected]@@[email protected]) 
1>main.obj : error LNK2019: unresolved external symbol "protected: void __thiscall boost::archive::basic_binary_iarchive<class boost::archive::binary_wiarchive>::load_override(struct boost::archive::class_name_type &,int)" ([email protected][email protected][email protected]@[email protected]@@[email protected]@@[email protected]@[email protected]) referenced in function "protected: void __thiscall boost::archive::binary_iarchive_impl<class boost::archive::binary_wiarchive,wchar_t,struct std::char_traits<wchar_t> >::load_override<struct boost::archive::class_name_type>(struct boost::archive::class_name_type &,int)" ([email protected][email protected]@[email protected]@@[email protected][email protected]@[email protected]@[email protected][email protected]@@@[email protected]@@[email protected]@[email protected]) 
1>main.obj : error LNK2019: unresolved external symbol "protected: void __thiscall boost::archive::basic_binary_oprimitive<class boost::archive::binary_woarchive,wchar_t,struct std::char_traits<wchar_t> >::save(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &)" ([email protected][email protected][email protected]@[email protected]@[email protected][email protected]@@@[email protected]@@[email protected][email protected]@[email protected]@[email protected]@[email protected]@[email protected]@@Z) referenced in function "public: static void __cdecl boost::archive::save_access::save_primitive<class boost::archive::binary_woarchive,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > >(class boost::archive::binary_woarchive &,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &)" ([email protected][email protected]@[email protected]@[email protected][email protected]@[email protected]@[email protected]@[email protected]@[email protected]@@[email protected]@[email protected]@[email protected]@[email protected][email protected]@[email protected]@[email protected]@[email protected]@[email protected]@@Z) 
1>main.obj : error LNK2019: unresolved external symbol "protected: void __thiscall boost::archive::basic_binary_oprimitive<class boost::archive::binary_woarchive,wchar_t,struct std::char_traits<wchar_t> >::save(class std::basic_string<wchar_t,struct std::char_traits<wchar_t>,class std::allocator<wchar_t> > const &)" ([email protected][email protected][email protected]@[email protected]@[email protected][email protected]@@@[email protected]@@[email protected][email protected][email protected]@@[email protected][email protected]@@[email protected]@@Z) referenced in function "public: static void __cdecl boost::archive::save_access::save_primitive<class boost::archive::binary_woarchive,class std::basic_string<wchar_t,struct std::char_traits<wchar_t>,class std::allocator<wchar_t> > >(class boost::archive::binary_woarchive &,class std::basic_string<wchar_t,struct std::char_traits<wchar_t>,class std::allocator<wchar_t> > const &)" ([email protected][email protected]@[email protected]@[email protected][email protected][email protected]@@[email protected][email protected]@@[email protected]@@[email protected]@[email protected]@[email protected]@[email protected][email protected][email protected]@@[email protected][email protected]@@[email protected]@@Z) 
1>main.obj : error LNK2019: unresolved external symbol "protected: void __thiscall boost::archive::basic_binary_iprimitive<class boost::archive::binary_wiarchive,wchar_t,struct std::char_traits<wchar_t> >::load(class std::basic_string<wchar_t,struct std::char_traits<wchar_t>,class std::allocator<wchar_t> > &)" ([email protected][email protected][email protected]@[email protected]@[email protected][email protected][email protected]@@[email protected]@@[email protected][email protected][email protected]@@[email protected][email protected]@@[email protected]@@Z) referenced in function "public: static void __cdecl boost::archive::load_access::load_primitive<class boost::archive::binary_wiarchive,class std::basic_string<wchar_t,struct std::char_traits<wchar_t>,class std::allocator<wchar_t> > >(class boost::archive::binary_wiarchive &,class std::basic_string<wchar_t,struct std::char_traits<wchar_t>,class std::allocator<wchar_t> > &)" ([email protected][email protected]@[email protected]@[email protected][email protected][email protected]@@[email protected][email protected]@@[email protected]@@[email protected]@[email protected]@[email protected]@[email protected][email protected][email protected]@@[email protected][email protected]@@[email protected]@@Z) 

諮詢不勝感激。

+0

要清楚,您是否實際鏈接到Boost.Serialization?狹窄的小溪是否適合你?您是自己構建Boost還是使用預先構建的二進制文件? – ildjarn

+0

前段時間我安裝了它(Boost),我相信我使用了Windows安裝程序,這顯然意味着它們已經建好了?無論哪種方式,'boost_1_47_0 \ stage \ lib'目錄中都有很多.lib文件。另外,你能否詳細說明狹義流的含義? – Interminable

+0

您下載的版本是否使用您正在使用的VC++的_exact_版本構建?而且你沒有用'/ Zc:wchar_t-'編譯?關於狹窄的溪流,對不起,這是一個錯字;我的意思是,爲你做些狹窄的_strings_工作?即,'std :: string'和'binary_oarchive'和'binary_oarchive'?或者這是明確的寬字符? – ildjarn

回答

3

事實證明,boost/archive/binary_woarchive.hppboost/archive/binary_wiarchive.hpp是多餘的。

而不是boost/archive/binary_oarchive.hppboost/archive/binary_iarchive.hpp即使對於包含寬字符變量的類實例也能正常工作!

我希望這些信息可以幫助某人。