使用 boost 序列化库序列化 stdext::hash_map
我想将哈希映射序列化为文件,并稍后将其反序列化。
#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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先,在代码顶部插入
#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 theresize
is one example of this difference .Next to resolve this new problem,
just
#include boost/serialization/map.hpp
and comment
s.resize(bucket_count);
inhash_collections_load_imp.hpp
( where it error )