vector<struct> serialization / deserialization



  • Hi Gentlement,

    I have ;

    struct objectv1 {
            matrix<float, 0, 1 > obj_descriptor;
            string person_name;
            matrix<rgb_pixel> p_obj_chip;
            string notes;
    
        };
    

    std::vector<objectv1> object_deteails;

    How can I serialize and deserialize this in Qt world ?

    stucked here for whole day ...

    thx



  • you have to create:

    QDataStream &operator<<(QDataStream &, const T&); //serialise
    QDataStream &operator>>(QDataStream &, T&); //de-serialise
    

    for T being:

    • std::vector
    • string
    • matrix
    • rgb_pixel
    • objectv1

    for example:

    template<class T>
    QDataStream &operator<<(QDataStream& stream, const std::vector<T>& val){
    stream << static_cast<quint32>(val.size());
    for(auto& singleVal : val)
    stream << singleVal;
    return stream;
    }
    template<class T>
    QDataStream &operator>>(QDataStream& stream, std::vector<T>& val){
    quint32 vecSize;
    val.clear();
    stream >> vecSize;
    val.reserve(vecSize);
    T tempVal;
    while(vecSize--){
    stream >> tempVal;
    val.push_back(tempVal);
    }
    return stream;
    }
    QDataStream &operator<<(QDataStream& stream, const objectv1& val){
    return stream << val.obj_descriptor << val.person_name << val.p_obj_chip << val.notes;
    }
    QDataStream &operator>>(QDataStream& stream, objectv1& val){
    return stream >> val.obj_descriptor >> val.person_name >> val.p_obj_chip >> val.notes;
    }
    

    I don't know what the other classes are so i'll leave it to you.



  • I will try and let you know .

    thnx a lot.



  • @VRonin said in vector<struct> serialization / deserialization:

    QDataStream &operator<<(QDataStream& stream, const objectv1& val){
    return stream << val.obj_descriptor << val.person_name << val.p_obj_chip << val.notes;
    }
    QDataStream &operator>>(QDataStream& stream, objectv1& val){
    return stream >> val.obj_descriptor >> val.person_name >> val.p_obj_chip >> val.notes;
    }

    it gave :

    ./widget.h:238:18: error: overloaded 'operator<<' must be a binary operator (has 3 parameters)
    QDataStream &operator<<(QDataStream& stream, const pobjectv1& val){
    ^

    I am missing something definitely. being a java girl for years , c++ looks like alien sometimes.. :)


  • Qt Champions 2017

    @RahibeMeryem said in vector<struct> serialization / deserialization:

    it gave :
    ./widget.h:238:18: error: overloaded 'operator<<' must be a binary operator (has 3 parameters)
    QDataStream &operator<<(QDataStream& stream, const pobjectv1& val){

    Show us what you got that generated that error, at least what is pobjectv1 and how you defined the operators. It's rather hard and futile to guess.



  • @kshegunov Hi . here the codes:

    struct personv1 {
            matrix<float, 0, 1 > obj_descriptor;
            string person_name;
            string notes;
            matrix<rgb_pixel> s_obj_chips;
           
        };
    std::vector<personv1> person;
    
    
    and:
    
    template<class T>
        QDataStream &operator<<(QDataStream& stream, const std::vector<T>& val){
            stream << static_cast<quint32>(val.size());
            for(auto& singleVal : val)
            stream << singleVal;
            return stream;
    }
        template<class T>
        QDataStream &operator>>(QDataStream& stream, std::vector<T>& val){
            quint32 vecSize;
            val.clear();
            stream >> vecSize;
            val.reserve(vecSize);
            T tempVal;
            while(vecSize--){
            stream >> tempVal;
            val.push_back(tempVal);
            }
            return stream;
    }
    
    QDataStream &operator<<(QDataStream& stream, const personv1& val){
        return stream << val.obj_descriptor << val.person_name << << val.notes << val.s_obj_chips ;
    
       }
    
        QDataStream &operator>>(QDataStream& stream, personv1& val){
        return stream >> val.obj_descriptor >> val.person_name >> val.notes >> val.s_obj_chips;
        }


  • You are missing the operators for : matrix, string and rgb_pixel I don't know what those classes are so i can't write them for you


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.