I want to serialize a hash map to a file and de-serialize it later on.
#include <boost/serialization/hash_map.hpp>
#include <boost/filesystem/fstream.hpp>
#include <hash_map>
class A: virtual public B {
public:
friend class boost::serialization::access;
stdext::hash_map<std::string, myClass> myClassHashTable;
template <class Archive>
void serialize(Archive &ar, const unsigned int version)
{
ar & myClassHashTable;
}
};
void A::serializedToDisk()
{
boost::filesystem::path finalPath(SOME_CONSTANT);
// code to create boost::filesystem::ifstream ofs object
boost::archive::text_oarchive oa(ofs);
oa << myClassHashTable;
}
void A::restoreFromDisk()
{
boost::filesystem::path finalPath(SOME_CONSTANT);
// code to create boost::filesystem::ifstream ifs object
boost::archive::text_iarchive ia(ifs);
ia >> myClassHashTable;
}
But I am getting an error as –
error C2039: ‘serialize’ : is not a member of ‘stdext::hash_map<_Kty,_Ty>’
I searched online for this error but didn’t get much help. Also, I checked in my boost installation serialization/hash_map.hpp does have a serialize() function in it. The same code worked for serialization of std::deque. Can anyone tell me how should I change it to make it compile?
First at all , insert
#define BOOST_HAS_HASHin top of your code .This change your compilation error to :
“error C2039: ‘resize’ : is not a member of ‘stdext::hash_map<_Kty,_Ty>’”. 😀
Next, if you comment your restoring function, you’ll see your code WORK fine and output ! < Good >
But the problem is about an incompatibility between compilers .
Unfortunately, the implementation of
hash_mapis different in ‘MSVS’ and ‘GCC’ and theresizeis one example of this difference .Next to resolve this new problem,
just
#include boost/serialization/map.hppand comment
s.resize(bucket_count);inhash_collections_load_imp.hpp( where it error )