Вопрос по c++ – Пример сериализации boost / archive / binary_woarchive.hpp и / или boost / archive / binary_wiarchive.hpp?

3

Я пытаюсь найти хороший пример того, как использовать эти двоичные широкие символьные версии сериализации Boost. Я собрал воедино некоторый код, чтобы попытаться заставить его работать, но, к сожалению, при попытке его скомпилировать меня забрасывают ошибки компоновщика.

Вот мой код, на случай, если я делаю что-то явно неправильное:

<code>    #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;
    }
</code>

Вот ошибки:

<code>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]@@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)
</code>

Советы по пониманию того, что не так и как решить эту проблему, будут с благодарностью.

Чтобы было ясно, вы на самом деле ссылаетесь на Boost.Serialization? У вас работают узкие потоки? Вы строили Boost самостоятельно или использовали готовые двоичные файлы? ildjarn
Я установил его (Boost) некоторое время назад, полагаю, что использовал установщик Windows, что, очевидно, означает, что они уже собраны? В любом случае в .lib-файлах многоboost_1_47_0\stage\lib каталог. Кроме того, вы можете уточнить, что вы подразумеваете под узкими потоками? Interminable
binary_oarchive а такжеbinary_iarchive Работай. Фактически я использовал код, который вставил выше, с некоторыми необходимыми изменениями для нормальных символов. Для/Z Я посмотрел на раздел командной строки проекта в VS2010 и/Zc:wchar_t есть. Interminable
И версия, которую вы скачали, была построена сexact версию VC ++, которую вы используете? И вы не компилируете с/Zc:wchar_t-? Что касается узких ручьев, извините, это была опечатка; Я имею в виду, сделать узкийstrings работа для тебя? т.е.,std::string а такжеbinary_oarchive а такжеbinary_oarchive? Или это определенно характерно для широких символов? ildjarn
Я только что проверил локально с вашим кодом и чистой сборкой Boost.Serialization 1.49.0 - я могу воспроизвести проблему. Я подозреваю, что это ошибка в Boost.Serialization, и я не думаю, что Роберт читает ТАК, поэтому вам лучше всего опубликовать вашу проблему наBoost Users mailing list (который он определенно читает). ildjarn

Ваш Ответ

1   ответ
3

boost/archive/binary_woarchive.hpp а такжеboost/archive/binary_wiarchive.hpp избыточны.

Вместоboost/archive/binary_oarchive.hpp а такжеboost/archive/binary_iarchive.hpp будет отлично работать даже для экземпляров классов, которые содержат переменные широких символов!

Я надеюсь, что эта информация кому-то поможет.

Похожие вопросы