2

オブジェクトをシリアル化してから逆シリアル化しようとしています。私が書いたものはすべて問題ないように見えますが、逆シリアル化中にまだエラーが発生しています。

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 のドキュメントが示唆するとおりにシリアライゼーションのコードを書きましたが、それでもエラーが発生します。それを手伝ってもらえますか?

編集:

明らかなように、最初は 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
4

1 に答える 1

1

BOOST_CLASS_EXPORT(); が必要だと思います。行。

たとえば、この質問に対する回答を参照してください:どこに BOOST_CLASS_EXPORT for boost::serialization を配置しますか?

于 2013-06-09T16:34:08.733 に答える