mardi 1 septembre 2015

C++ boost Tuple Serialization/Deserialization

it is probably really simple but I am stock on this. I am trying to serialize and deserialize boost::tuple. I wrote a "serialize" function and it perfectly serializes the tuple. However, I do not have any "deserialize" function. I am not even sure do I need one. Most of the examples do not even have one. Here is my code,

namespace boost
{
    namespace serialization
    {
        template<typename Archive, typename T1, typename T2>
        void serialize(Archive & ar, boost::tuple<T1, T2> & t,
                    const unsigned int)
        {
            ar & t.get<0>();
            ar & t.get<1>();
        }

    }
}

using namespace std;
using namespace boost;

int main() {
    std::ofstream os("archive.txt");
    {

        boost::tuple<int, int> t = boost::make_tuple(1, 5);
        boost::archive::text_oarchive ar(os);
        ar & boost::serialization::make_nvp("first", t.get<0>() );
        ar & boost::serialization::make_nvp("second", t.get<1>() );
    }

    {
        std::ifstream file("archive.txt");
        boost::archive::text_iarchive ia(file);
        boost::tuple<int, int> t;
            ia >> t;
            std::cout << "First: " << t.get<0>() << "\n" << "Second: " << t.get<1>() << std::endl;
    }
    return 0;
}

I get below error,

Undefined symbols for architecture x86_64: "boost::archive::text_iarchive_impl
<boost::archive::text_iarchive>::load_override(boost::archive::class_name_type&, int)", referenced from: boost::archive::text_iarchive& boost::archive::detail::interface_iarchive
  <boost::archive::text_iarchive>::operator>>
    <boost::archive::class_name_type>(boost::archive::class_name_type&) in cc4Y8n8F.o ld: symbol(s) not found for architecture x86_64 collect2: error: ld returned 1 exit status

How can I overcome this error?

boost::archive::text_iarchive ia(file); line causes the error

Aucun commentaire:

Enregistrer un commentaire