2

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?

Emadpres
  • 3,466
  • 2
  • 29
  • 44
Onkar Deshpande
  • 301
  • 4
  • 15
  • Not quite what you want, but Google Sparsehash library maps come with some built-in serialization support, though completely separate from Boost serialization. –  Sep 07 '10 at 20:09
  • http://stackoverflow.com/questions/23764249/c-boost-serialization-error-for-hash-map-with-custom-objects-as-key/23768271#23768271 – sehe Jan 10 '15 at 16:57

1 Answers1

4

First at all , insert #define BOOST_HAS_HASH in top of your code .

This change your compilation error to :

“error C2039: 'resize' : is not a member of 'stdext::hash_map<_Kty,_Ty>'”. :D

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_map is different in 'MSVS' and 'GCC' and the resize is one example of this difference .


Next to resolve this new problem, just #include boost/serialization/map.hpp and comment s.resize(bucket_count); in hash_collections_load_imp.hpp ( where it error )

Emadpres
  • 3,466
  • 2
  • 29
  • 44