1

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

Piotr Skotnicki
  • 46,953
  • 7
  • 118
  • 160
Güngör Basa
  • 628
  • 2
  • 9
  • 27
  • You do not need a separate deserialize here. Iff you do, you need to "split" the serialization into `load()` and `save()` (see the documentation) – sehe Sep 01 '15 at 09:45

1 Answers1

0

You need to qualify template members inside a dependent type:

        ar & t.template get<0>();
        ar & t.template get<1>();

See also Where and why do I have to put the "template" and "typename" keywords?

Alternatively, use the free function get:

 ar & get<0>(t) & get<1>(t);
Community
  • 1
  • 1
sehe
  • 374,641
  • 47
  • 450
  • 633
  • Added the more idiomatic approach. – sehe Sep 01 '15 at 09:44
  • you mean in my serialize function right. I did that but I still have the same error. @sehe – Güngör Basa Sep 01 '15 at 17:39
  • The only difference is name of .o file. It was "cc4Y8n8F.o ", now it is "cceAImXx.o". I also tried bunch of different people's example and, they did not work either. – Güngör Basa Sep 01 '15 at 18:20
  • I also tried the same code with different compiler in a different computer, this time it compiles but I get, "terminate called after throwing an instance of 'boost::archive::archive_exception' what(): class version Abort (core dumped)" error – Güngör Basa Sep 01 '15 at 18:26