C++ Boost序列化输入流错误
我正在尝试序列化派生类的成员。此成员是从基类继承的。序列化代码以非侵入方式包含在基类头文件中。save()、load()方法是基类实现的一部分。我在尝试运行代码时遇到以下错误:C++ Boost序列化输入流错误,c++,boost,boost-serialization,C++,Boost,Boost Serialization,我正在尝试序列化派生类的成员。此成员是从基类继承的。序列化代码以非侵入方式包含在基类头文件中。save()、load()方法是基类实现的一部分。我在尝试运行代码时遇到以下错误: boost::archive::archive\u异常“what():输入流错误 以下是守则的相关部分: modelbrary.h(这是基类) 型号库4GPCS.cpp bool ModelLibrary::saveHashTable () { string filename; generateHashTable
boost::archive::archive\u异常“what():输入流错误
以下是守则的相关部分:
modelbrary.h(这是基类)
型号库4GPCS.cpp
bool
ModelLibrary::saveHashTable ()
{
string filename;
generateHashTableArchiveName (filename);
cout << "archive filename" << filename;
std::ofstream ofs(filename.c_str());
boost::archive::text_oarchive oa(ofs);
// write class instance to archive
oa << shash_table_;
// archive and stream closed when destructors are called
return true;
}
bool
ModelLibrary::loadHashTable ()
{
string filename;
generateHashTableArchiveName (filename);
std::ifstream ifs(filename.c_str());
boost::archive::text_iarchive ia(ifs);
ia >> shash_table_;
return true;
}
bool
ModelLibraryG4PCS::generateHashTableArchiveName (string &filename)
{
filename = "";
//Concatenate all Added Models into filename
std::map<std::string, Model*>::const_iterator model_itr = models_.begin();
for (; model_itr != models_.end(); ++model_itr)
{
filename += model_itr->first;
}
// No Models Have been added
if (filename == "")
return false;
std::stringstream ss;
ss << pair_width_ << "_" << dipole_width_ << "_" << voxel_size_ << ".hash";
filename += ss.str();
return true;
}
int main()
{
ModelLibraryG4PCS d;
if (!d.loadHashTable()) {
/* Some Code to Construct a new Hash Table */
d.saveHashTable();
}
}
您的示例代码不是独立的,所以我只能浏览一下 目前:
for(int i = 0; i < h.total_num_of_voxels_; ++i)
ar & h.voxels_[i];
ar & h.num_of_voxels_;
ar & h.total_num_of_voxels_i_;
下面是一个反向工程示例,它根据您的原始代码进行一些猜测:
输出
g++ -std=c++11 -Os -pedantic main.cpp -lboost_system -lboost_serialization && ./a.out && tail *.hash
archive filenameonetwo42_4242_999.hash
22 serialization::archive 10 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
你永远不会再为一个问题发布残缺的代码了。我刚刚花了45分钟计算出,您将
modelbrary
随机变异为base
,并将modelbraryg4pcs
变异为Derived
。这并不好笑,完全没有必要(它浪费了我们所有的时间)。这就是说,下次让它自我包含:。很抱歉,我用派生和based解决了这个问题。我用显示八叉树节点类定义的自包含代码问了另一个问题。这里的序列化代码在这种情况下不起作用。问题是:
for(int i = 0; i < h.total_num_of_voxels_; ++i)
ar & h.voxels_[i];
ar & h.num_of_voxels_;
ar & h.total_num_of_voxels_i_;
ar & boost::serialization::make_array(h.voxels_, h.total_num_of_voxels_);
ar & h.num_of_voxels_;
g++ -std=c++11 -Os -pedantic main.cpp -lboost_system -lboost_serialization && ./a.out && tail *.hash
archive filenameonetwo42_4242_999.hash
22 serialization::archive 10 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0