Boost logo

Boost Users :

Subject: [Boost-users] serialization std::map
From: Adam Szeliga (aszeliga.xsoftware_at_[hidden])
Date: 2009-07-16 19:16:32


Why I can not input from archive std::map ?

line: inputArchive >> inputContainer causes compile error:

Thanks
Adam

#include <string>
#include <iostream>
#include <fstream>
#include <sstream>
#include <map>
#include <boost/serialization/map.hpp>
#include <boost/serialization/serialization.hpp>
#include <boost/serialization/string.hpp>
#include <boost/serialization/level.hpp>
#include <boost/serialization/tracking.hpp>
#include <boost/serialization/split_free.hpp>
#include <boost/serialization/base_object.hpp>
#include <boost/archive/text_iarchive.hpp>
#include <boost/archive/text_oarchive.hpp>
#include <boost/archive/text_wiarchive.hpp>
#include <boost/archive/text_woarchive.hpp>

class A
{
public:
    A() {};
    A(std::string const & s):
        s(s)
    {
    }

    virtual ~A() {};

private:
    std::string s;

    friend std::ostream & operator<<(std::ostream & os, A const & a);

    friend class boost::serialization::access;
    template<class Archive>
    void serialize(Archive & archive, unsigned int const version)
    {
        archive & s;
    }
};

std::ostream & operator<<(std::ostream & os, A const & a)
{
    return os << a.s;
}

typedef std::map<unsigned int const, A * const> Container;

BOOST_CLASS_TRACKING(Container, boost::serialization::track_never);

std::ostream & operator<<(std::ostream & os, Container const & container)
{
    Container::const_iterator end = container.end();
    for (Container::const_iterator i = container.begin(); i != end; ++i)
    {
        os << "(" << i->first << "," << *i->second << ")" << std::endl;
    }
    return os;
}

int _tmain(int argc, _TCHAR* argv[])
{
    std::cout << "serialization map test starts" << std::endl;

    Container outputContainer;

    outputContainer.insert(std::make_pair(1, new A("one")));
    outputContainer.insert(std::make_pair(4, new A("four")));
    outputContainer.insert(std::make_pair(2, new A("two")));

    std::cout << outputContainer << std::endl;

    {
        std::wofstream outputFile("data.txt");
        boost::archive::text_woarchive outputArchive(outputFile);

        outputArchive << outputContainer;
    }
   
    std::cout << "---------------------------" << std::endl;

    Container inputContainer;
    {
        std::wifstream inputFile("data.txt");
        boost::archive::text_wiarchive inputArchive(inputFile);

        inputArchive >> inputContainer; // this line causes compile error
    }

    std::cout << inputContainer << std::endl;

    std::cout << "serialization map test ends" << std::endl;

    return 0;
}


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net