c++ Boost反序列化what():输入流错误

C++ Boost deserialization what(): input stream error

本文关键字:输入流 错误 Boost 反序列化 what c++      更新时间:2023-10-16

我正在尝试对对象进行序列化,然后对其进行反序列化。尽管看起来我写的一切都很好,但在反序列化过程中我仍然得到一个错误。

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?