gpt4 book ai didi

c++ - 使用 boost 序列化到磁盘后无法加载回数据

转载 作者:行者123 更新时间:2023-11-30 04:28:38 26 4
gpt4 key购买 nike

我能够使用 boost 序列化将我的数据保存到磁盘上。但是,我无法取回数据。你能告诉我我做错了什么吗?

谢谢,

下面是我的代码

void nDB::save_macros(string filename) {
std::ofstream ofs(filename.c_str(), std::ios::out | std::ios::binary);
//assert(ofs.good());
boost::archive::binary_oarchive oa(ofs);
oa << this;
}

void nDB::load_macros(string filename) {
std::ifstream ifs(filename.c_str());
//assert(ifs.good());
boost::archive::binary_iarchive ia(ifs);
nDB *db = new nDB;
ia >> db;
*this = *db;
}

下面是我的序列化实例

template<class Archive>
void nDB::serialize(Archive &ar, const unsigned int version) {
boost::unordered_map<string,macro*,myhash,cmp_str>::iterator M_IT;
boost::unordered_map<string,layer*,myhash,cmp_str>::iterator L_IT;
for (L_IT = _LAYERS.begin();L_IT != _LAYERS.end();L_IT++) {
string tmpstr = L_IT->first;
//ar & L_IT->first;
ar & tmpstr;
ar & *(L_IT->second);
}
for (M_IT = _MACROS.begin();M_IT != _MACROS.end();M_IT++) {
string tmpstr = M_IT->first;
//ar & M_IT->first;
ar & tmpstr;
ar & *(M_IT->second);
}
}

下面是我的运行结果保存运行:

Insert macro mac1 into database OK!
Insert macro mac2 into database OK!
Insert macro mac3 into database OK!
Insert port P1 OK!
Insert port P2 OK!
Insert port P2 OK!
Insert port P3 OK!
Insert port P1 OK!
Insert port P3 OK!
Layer mac3 is found
Macro mac3 has these port:
Port P3 is found
Port P1 is found
Port P3 of macro mac3 has use CLOCK and dir INOUT
Port P1 of macro mac3 has use POWER and dir OUTPUT

下面是我加载运行的结果

ERROR:: Could not find macro mac3 in database

最佳答案

我认为您在 nDB::serialize 方法中做的太多了。

Boost Serialize 将愉快地为您序列化您的 map ,如果您只是为您的 类提供serialize 方法。一旦你写完这些,你应该能够将给定的方法简化为如下所示:

template<class Archive>
void nDB::serialize(Archive &ar, const unsigned int version) {
ar & _LAYERS;
ar & _MACROS;
}

关于c++ - 使用 boost 序列化到磁盘后无法加载回数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9986865/

26 4 0