使用 boost 序列化库序列化 stdext::hash_map

发布于 2024-09-17 07:27:38 字数 1226 浏览 5 评论 0原文

我想将哈希映射序列化为文件,并稍后将其反序列化。

#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;
}

但我收到一个错误 -

错误 C2039:“序列化”:不是“stdext::hash_map<_Kty,_Ty>”的成员

我在网上搜索了这个错误,但没有得到太多帮助。另外,我检查了我的 boost 安装 serialization/hash_map.hpp 中确实有一个 serialize() 函数。相同的代码适用于 std::deque 的序列化。谁能告诉我应该如何更改它才能编译?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

池木 2024-09-24 07:27:39

首先,在代码顶部插入 #define BOOST_HAS_HASH

这会将您的编译错误更改为:

“错误 C2039: 'resize' : 不是 'stdext::hash_map<_Kty,_Ty>' 的成员”。 :D

接下来,如果您评论您的恢复功能,您将看到您的代码工作正常并输出! <好>

但问题在于编译器之间的不兼容性。
不幸的是,hash_map 的实现在“MSVS”和“GCC”中是不同的,resize 就是这种差异的一个例子。


接下来要解决这个新问题,
只是#include boost/serialization/map.hpp
并在 hash_collections_load_imp.hpp 中注释 s.resize(bucket_count); (出现错误的地方)

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 )

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文