C++ Boost 反序列化 what() : input stream error

标签 c++ serialization boost deserialization

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

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

最佳答案

我觉得你需要 BOOST_CLASS_EXPORT(); 线。

例如,请参阅此问题的答案:Where to put BOOST_CLASS_EXPORT for boost::serialization?

关于C++ Boost 反序列化 what() : input stream error,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16896751/

相关文章:

java - 对象序列化不起作用

C++ boost : Any gotchas with BOOST_FOREACH?

c++ - 单例中静态声明之间的差异

c++ - 是否可以在 Linux 中以编程方式单击另一个应用程序的按钮?

c++ - 如何将包含嵌入空值的 Google Protocol Buffer SerializeAsString() 输出分配给 std::string?

.net - 如何仅序列化某些对象属性

c++ - Boost.Intrusive Containers - 不同大小的元素

php - 使用 Boost::Process 运行 php 命令

c++ - 2系高效电源 : (2^n) + (2^(n-1)) + (2^(n-2))

C++ CppCheck 算法建议(std::find_if 而不是原始循环)相关性