序列化指针本身不是目标对象

时间:2013-03-13 06:16:31

标签: c++ boost boost-serialization

我有一个数据类要使用boost序列化程序保存和加载。

该类包括两个成员En *senderEn *receiver两个已在我的系统中创建的对象。 我不需要再次创建它们。我只需要发送(序列化)他们的address,作为另一端的参考。

如果我使用像En *senderEn *receiver这样的普通指针,boost将序列化整个对象(我不想要)。

所以我认为我应该使用产生错误的En **senderEn **receiver。我可以知道如何修改课程以达到我的目的吗?

非常感谢。

class dataMessage
{

    friend class boost::serialization::access;
    template<class Archive>
    void serialize(Archive &ar, const unsigned int version)
    {            
        ar & sender;
        ar & receiver;
    }
    //I figured I should user pointer to pointer coz in case of the normal pointer,
    //the serilizer would serializes the object that the pointer is pointing to;
    //whereas I just need to save the 'pointer' to object so that we
    //can use it as a reference at the other end.
    En **sender;
    En **receiver;
public:
    dataMessage(){
        sender = receiver = 0;
    }
    void setDataClassType();
    virtual void registerType(boost::archive::text_oarchive &oa)
    {
        oa.register_type(static_cast<dataMessage *>(NULL));
    }

    virtual void registerType(boost::archive::text_iarchive &ia)
    {
        ia.register_type(static_cast<dataMessage *>(NULL));
    }
};

错误的一部分:

error: request for member ‘serialize’ in ‘t’, which is of pointer type ‘En*’ (maybe you meant to use ‘->’ ?)

1 个答案:

答案 0 :(得分:0)

似乎没有办法手动存储地址,否则boostserializer将序列化目标对象。所以我将发送方接收方类型修改为:

class dataMessage
{

 //......
public:
//.....
    //a proper size of integer is used coz in case of the normal pointer,
    //the serilizer serializes the object that the pointer is pointing to
    //whereas we just need to save the 'pointer' to object so that we
    //can use it as a reference at the other end. so instead of storing 'sim_mob::Entity *'
    //we store:
#if __x86_64__  //64 bit
    unsigned long sender;
    unsigned long receiver;
#else          //32 bit
    unsigned int sender;
    unsigned int receiver;

#endif


    dataMessage(){
        sender = receiver = 0;
    }
//...
};

稍后使用指针时,我会对适当的指针类型进行简单的转换。