c++ Boost反序列化what():输入流错误
C++ Boost deserialization what(): input stream error
我正在尝试对对象进行序列化,然后对其进行反序列化。尽管看起来我写的一切都很好,但在反序列化过程中我仍然得到一个错误。
int main(){
MapAttributes mapAtt(MAP_SIZE,MAP_SIZE);
initMapFromImage("map1.png",&mapAtt);
int prevxMax = mapAtt.prevxMax ;
int prevyMax = mapAtt.prevyMax ;
int prevxMin = mapAtt.prevxMin ;
int prevyMin = mapAtt.prevyMin ;
PixelCoords pose = PixelCoords(mapAtt.robotPose.dx, mapAtt.robotPose.dy);
PixelCoords target = PixelCoords( 2048+250, 2048+250 );
PartitionGraphNodes partitionGraph(&mapAtt);
PartitionGraphNodes partitionGraph2(&mapAtt);
partitionGraph.createIncrementalPartition(pose,target);
if (partitionGraph.nodes.size()!=0){
saveSerializedObject<PartitionGraphNodes(partitionGraph,"partition_graph_marshall");
std::cout << "size= " << partitionGraph.nodes.size() << "n";
restoreSerializedObject<PartitionGraphNodes>(partitionGraph2,"partition_graph_marshall");
}
else{
std::cout << "RUN FOR YOUR LIVES!!!n";
}
return 0;
这是我的代码,我试图让我的对象序列化和反序列化。
下面是序列化和反序列化的函数:
template<class T>
void saveSerializedObject(const T &s, const char * filename){
// make an archive
std::ofstream ofs(filename);
boost::archive::text_oarchive oa(ofs);
oa << s;
}
template<class T>
void restoreSerializedObject(T &s, const char * filename)
{
// open the archive
std::ifstream ifs(filename);
if (ifs.good()) {
std::cout << "Coming!!!n";
boost::archive::text_iarchive ia(ifs);
ia >> s;
} else {
// throw an error or something
assert(false);
}
}
最后,我完全按照Boost文档的建议编写了序列化代码,但我仍然得到一个错误:' Boost::archive::archive_exception'的实例what(): input stream error。你能帮我吗?
编辑:很明显,最初我试图序列化类PartitionGraphNodes的对象。下面是PartitionGraphNodes对象的序列化方式:
#include <boost/serialization/base_object.hpp>
class PartitionGraphNodes: public NodesVector{
private:
std::vector<int> targetNeighbors; //!<Vector holding target neighbors
std::vector<int> robotNeighbors; //!<Vector holding robot neighbors
std::vector<PixelCoords> uniformPartition;
public:
PartitionGraphNodes(MapAttributes* mapAttr);
void createIncrementalPartition(PixelCoords startPos, PixelCoords target);
int insertNodeInPartition(Node currNode);
template<class Archive>
void serialize(Archive & ar, const unsigned int version)
{
// serialize base class information
std::cout << "In partition_serial" << "n";
ar & boost::serialization::base_object<NodesVector>(*this);
}
};
#include <boost/serialization/vector.hpp>
class NodesVector{
protected:
//~ std::vector<Voronode> nodes;
public:
NodesVector(void){};
std::vector<Node> nodes;
MapAttributes* mapAttributes;
template<class Archive>
void serialize(Archive & ar, const unsigned int version)
{
std::cout << "In nodesVector_serial" << "n";
ar & nodes;
}
};
#include <boost/serialization/vector.hpp>
class Node{
public:
PixelCoords p; //!< The coordinates of the node
bool visited; //!< True if the node has been visited
unsigned int ID; //!< The node's ID
int parent;
std::vector<PixelCoords> neigh; //!< Vector of the neighbors of the node (coords)
std::vector<unsigned int> neighID; //!< Vector of the neighbors' IDs
std::vector<float> dist; //!< Vector of the distances of the neighbors
Weight w; //!< The node's weight
std::vector<PixelCoords> path;
Node(PixelCoords a) {p=a;}
Node(PixelCoords a,unsigned int IDt) {p=a; ID=IDt;}
Node(void){}
void makeNeighbor(Node &a);
template<class Archive>
void serialize(Archive & ar, const unsigned int version)
{
std::cout << "In nodes_serial" << "n";
ar & p;
ar & visited;
ar & ID;
ar & parent;
ar & neigh;
ar & neighID;
ar & dist;
ar & w;
}
};
#endif
我想你需要BOOST_CLASS_EXPORT ();行。
请看下面这个问题的答案:在哪里放置BOOST_CLASS_EXPORT for boost::serialization?
相关文章:
- 如何忽略输入流中的某些字符?
- 张量流错误 此文件需要编译器和库支持 ISO C++ 2011 标准
- C++输入流.与 cin unget() 函数有点混淆
- 从输入流中读取行,并能够跳过块
- C++ 错误检查 fstream open() 命令和一般字符串流错误处理
- 直接输入编译错误 (C++)
- 使用 Visual Studio 代码读取 C++ 中的输入流时出错
- 处理输入流运算符>>中的无效输入
- 在课堂上创建了一个朋友,给出"无输入文件"错误
- C++ 中连接 4 游戏的输入逻辑错误
- 树莓派上的端口音频,找不到输入流的设备
- 输入类型错误
- 如何将C++输入流分隔符包含在结果令牌中
- 提升输入流错误.如何将标准:矢量的动态矢量另存为 XML 文件
- C++Boost序列化:输入流错误
- 提升反序列化问题:运行时输入流错误 (c++)
- getline 和在类函数中传递输入流,主表达式错误
- 提升序列化:向前兼容失败,出现输入流错误
- c++ Boost反序列化what():输入流错误
- Boost序列化输入流错误