I wrote a class with a member that is a nested unordered_map with a custom type as key and another unordered_map as value.
I would like to serialize/deserialize the inner map using the boost::serialization library.
However, even if the code is mostly done, the compiler returns this error code (I cut it down and simplified it a bit):
class std::unordered_map<
const MyClass<...>::key_type,
unsigned int,
MyClass<...>::key_hash,
std::equal_to<const MyClass<...>::key_type>,
std::allocator<std::pair<const MyClass<...>::key_type, unsigned int> > >’ has no member named ‘serialize’
From what I understand, it tells me that there is no member serialize for this map. However, I included the boost headers that should fix this (but it is not).
I suspect that the fact I declared the key as being const in the map declaration messes with the library, but I'm unable (aka not knowledgeable enough) to make sure of it: when I remove the const qualifier I end up with a bunch of other compilation errors.
The code for my class is the following:
#include <boost/serialization/serialization.hpp>
#include <boost/serialization/unordered_map.hpp>
#include <boost/serialization/utility.hpp>
#include <boost/archive/binary_iarchive.hpp>
#include <boost/archive/binary_oarchive.hpp>
#include <unordered_map>
template<typename A, typename B, typename C>
class MyClass
{
public:
using coord_type = another_serializable_and_tested_class;
struct key_type
{
coord_type from;
coord_type to;
key_type(coord_type const& origin, coord_type const& destination):
from(origin),
to(destination)
{}
bool operator==(key_type const& other) const
{
return (
other.from == this->from &&
other.to == this->to
);
}
friend class boost::serialization::access;
template<class Archive>
void serialize(Archive & ar, const unsigned int version)
{
ar & from;
ar & to;
}
}; // end struct key_type
struct key_hash : public std::unary_function<key_type, std::size_t>
{
std::size_t operator()(const key_type& k) const
{
size_t res = 17;
res = res * 31 + std::hash<coord_type>()( k.from );
res = res * 31 + std::hash<coord_type>()( k.to );
return res;
}
}; // end struct key_hash
using forward_flow_type = std::unordered_map<time_type, std::unordered_map<const key_type, value_type, key_hash>>;
// other members and functions, including the serialization/deserialization operations:
void deserialize_layer(int t) const
{
const std::string forward_filename = get_forward_flow_archive_name(t);
// create and open an archive for input
std::ifstream forward_ifs(forward_filename, std::ios::binary);
boost::archive::binary_iarchive forward_ia(forward_ifs);
// read class state from archive
std::unordered_map<const key_type, value_type, key_hash> forward_layer;
forward_ia >> forward_layer;
// archive and stream closed when destructors are called
m_forward_flow.emplace(t,forward_layer);
}
};
Any idea what is going wrong?