【问题标题】:C++ Boost deserialization what(): input stream errorC++ Boost反序列化what():输入流错误
【发布时间】:2013-06-03 12:29:58
【问题描述】:

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

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(): 输入流错误。你能帮我解决这个问题吗?

编辑:

很清楚,最初我试图序列化 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

【问题讨论】:

  • 请帮助我们为您提供帮助 - 编写一个最低限度完整的示例来重现该问题。
  • 我已经在我的帖子中进行了适当的编辑。我希望现在已经足够清楚了......

标签: c++ serialization boost deserialization


【解决方案1】:

我认为你需要 BOOST_CLASS_EXPORT(); 线。

例如查看这个问题的答案:Where to put BOOST_CLASS_EXPORT for boost::serialization?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-04-14
    • 1970-01-01
    • 1970-01-01
    • 2011-08-28
    • 1970-01-01
    • 1970-01-01
    • 2016-05-05
    相关资源
    最近更新 更多