问题
I'm trying to find a good example of how to use these binary wide character versions of boost's serialization stuff. I pieced together some code to try and get it working, but unfortunately I get bombarded with linker errors when trying to compile it.
Here's my code, in case I'm doing anything obviously wrong:
#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;
}
Here are the errors:
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)" (??1?$basic_binary_oprimitive@Vbinary_woarchive@archive@boost@@_WU?$char_traits@_W@std@@@archive@boost@@IAE@XZ) 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)" (??1?$binary_oarchive_impl@Vbinary_woarchive@archive@boost@@_WU?$char_traits@_W@std@@@archive@boost@@QAE@XZ)
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)" (??1?$basic_binary_iprimitive@Vbinary_wiarchive@archive@boost@@_WU?$char_traits@_W@std@@@archive@boost@@IAE@XZ) 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)" (??1?$binary_iarchive_impl@Vbinary_wiarchive@archive@boost@@_WU?$char_traits@_W@std@@@archive@boost@@QAE@XZ)
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)" (??0?$basic_binary_oprimitive@Vbinary_woarchive@archive@boost@@_WU?$char_traits@_W@std@@@archive@boost@@IAE@AAV?$basic_streambuf@_WU?$char_traits@_W@std@@@std@@_N@Z) 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)" (??0?$binary_oarchive_impl@Vbinary_woarchive@archive@boost@@_WU?$char_traits@_W@std@@@archive@boost@@IAE@AAV?$basic_ostream@_WU?$char_traits@_W@std@@@std@@I@Z)
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)" (??0?$basic_binary_iprimitive@Vbinary_wiarchive@archive@boost@@_WU?$char_traits@_W@std@@@archive@boost@@IAE@AAV?$basic_streambuf@_WU?$char_traits@_W@std@@@std@@_N@Z) 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)" (??0?$binary_iarchive_impl@Vbinary_wiarchive@archive@boost@@_WU?$char_traits@_W@std@@@archive@boost@@IAE@AAV?$basic_istream@_WU?$char_traits@_W@std@@@std@@I@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> >::init(void)" (?init@?$basic_binary_oprimitive@Vbinary_woarchive@archive@boost@@_WU?$char_traits@_W@std@@@archive@boost@@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)" (?init@?$binary_oarchive_impl@Vbinary_woarchive@archive@boost@@_WU?$char_traits@_W@std@@@archive@boost@@IAEXI@Z)
1>main.obj : error LNK2019: unresolved external symbol "protected: void __thiscall boost::archive::basic_binary_oarchive<class boost::archive::binary_woarchive>::init(void)" (?init@?$basic_binary_oarchive@Vbinary_woarchive@archive@boost@@@archive@boost@@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)" (?init@?$binary_oarchive_impl@Vbinary_woarchive@archive@boost@@_WU?$char_traits@_W@std@@@archive@boost@@IAEXI@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> >::init(void)" (?init@?$basic_binary_iprimitive@Vbinary_wiarchive@archive@boost@@_WU?$char_traits@_W@std@@@archive@boost@@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)" (?init@?$binary_iarchive_impl@Vbinary_wiarchive@archive@boost@@_WU?$char_traits@_W@std@@@archive@boost@@IAEXI@Z)
1>main.obj : error LNK2019: unresolved external symbol "protected: void __thiscall boost::archive::basic_binary_iarchive<class boost::archive::binary_wiarchive>::init(void)" (?init@?$basic_binary_iarchive@Vbinary_wiarchive@archive@boost@@@archive@boost@@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)" (?init@?$binary_iarchive_impl@Vbinary_wiarchive@archive@boost@@_WU?$char_traits@_W@std@@@archive@boost@@IAEXI@Z)
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)" (?load_override@?$basic_binary_iarchive@Vbinary_wiarchive@archive@boost@@@archive@boost@@IAEXAAUclass_name_type@23@H@Z) 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)" (??$load_override@Uclass_name_type@archive@boost@@@?$binary_iarchive_impl@Vbinary_wiarchive@archive@boost@@_WU?$char_traits@_W@std@@@archive@boost@@IAEXAAUclass_name_type@12@H@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<char,struct std::char_traits<char>,class std::allocator<char> > const &)" (?save@?$basic_binary_oprimitive@Vbinary_woarchive@archive@boost@@_WU?$char_traits@_W@std@@@archive@boost@@IAEXABV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@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 &)" (??$save_primitive@Vbinary_woarchive@archive@boost@@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@save_access@archive@boost@@SAXAAVbinary_woarchive@12@ABV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@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 &)" (?save@?$basic_binary_oprimitive@Vbinary_woarchive@archive@boost@@_WU?$char_traits@_W@std@@@archive@boost@@IAEXABV?$basic_string@_WU?$char_traits@_W@std@@V?$allocator@_W@2@@std@@@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 &)" (??$save_primitive@Vbinary_woarchive@archive@boost@@V?$basic_string@_WU?$char_traits@_W@std@@V?$allocator@_W@2@@std@@@save_access@archive@boost@@SAXAAVbinary_woarchive@12@ABV?$basic_string@_WU?$char_traits@_W@std@@V?$allocator@_W@2@@std@@@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> > &)" (?load@?$basic_binary_iprimitive@Vbinary_wiarchive@archive@boost@@_WU?$char_traits@_W@std@@@archive@boost@@IAEXAAV?$basic_string@_WU?$char_traits@_W@std@@V?$allocator@_W@2@@std@@@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> > &)" (??$load_primitive@Vbinary_wiarchive@archive@boost@@V?$basic_string@_WU?$char_traits@_W@std@@V?$allocator@_W@2@@std@@@load_access@archive@boost@@SAXAAVbinary_wiarchive@12@AAV?$basic_string@_WU?$char_traits@_W@std@@V?$allocator@_W@2@@std@@@Z)
Advice on understanding what is wrong and how to resolve it would be greatly appreciated.
回答1:
It turns out that boost/archive/binary_woarchive.hpp
and boost/archive/binary_wiarchive.hpp
are redundant.
Instead boost/archive/binary_oarchive.hpp
and boost/archive/binary_iarchive.hpp
will work just fine even for class instances that contain wide character variables!
I hope this information helps someone.
来源:https://stackoverflow.com/questions/10439984/serialization-example-of-boost-archive-binary-woarchive-hpp-and-or-boost-archive