boost / archive / binary_woarchive.hpp和/或boost / archive / binary_wiarchive.hpp的序列化示例?

时间:2012-05-03 22:07:25

标签: c++ linker-errors boost-serialization wofstream

我正在尝试找到一个很好的例子来说明如何使用这些二进制宽字符版本的boost的序列化内容。我拼凑了一些代码来尝试让它工作,但不幸的是,当我尝试编译它时,我会被链接器错误轰炸。

这是我的代码,以防我做任何明显错误的事情:

    #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)" (??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)

对于理解错误以及如何解决问题的建议将不胜感激。

1 个答案:

答案 0 :(得分:3)

事实证明,boost/archive/binary_woarchive.hppboost/archive/binary_wiarchive.hpp是多余的。

相反,boost/archive/binary_oarchive.hppboost/archive/binary_iarchive.hpp即使对于包含宽字符变量的类实例也会正常工作!

我希望这些信息有助于某人。