Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. vector<struct> serialization / deserialization
Forum Updated to NodeBB v4.3 + New Features

vector<struct> serialization / deserialization

Scheduled Pinned Locked Moved Unsolved General and Desktop
7 Posts 3 Posters 3.1k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    RahibeMeryem
    wrote on 9 Feb 2018, 20:44 last edited by VRonin 2 Sept 2018, 21:07
    #1

    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

    1 Reply Last reply
    0
    • V Offline
      V Offline
      VRonin
      wrote on 9 Feb 2018, 21:19 last edited by
      #2

      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.

      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
      ~Napoleon Bonaparte

      On a crusade to banish setIndexWidget() from the holy land of Qt

      1 Reply Last reply
      5
      • R Offline
        R Offline
        RahibeMeryem
        wrote on 10 Feb 2018, 14:07 last edited by
        #3

        I will try and let you know .

        thnx a lot.

        1 Reply Last reply
        0
        • R Offline
          R Offline
          RahibeMeryem
          wrote on 10 Feb 2018, 18:01 last edited by
          #4

          @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.. :)

          K 1 Reply Last reply 11 Feb 2018, 22:43
          0
          • R RahibeMeryem
            10 Feb 2018, 18:01

            @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.. :)

            K Offline
            K Offline
            kshegunov
            Moderators
            wrote on 11 Feb 2018, 22:43 last edited by
            #5

            @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.

            Read and abide by the Qt Code of Conduct

            R 1 Reply Last reply 12 Feb 2018, 17:21
            2
            • K kshegunov
              11 Feb 2018, 22:43

              @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.

              R Offline
              R Offline
              RahibeMeryem
              wrote on 12 Feb 2018, 17:21 last edited by RahibeMeryem 2 Dec 2018, 17:27
              #6

              @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;
                  }
              1 Reply Last reply
              0
              • V Offline
                V Offline
                VRonin
                wrote on 12 Feb 2018, 18:19 last edited by VRonin 2 Dec 2018, 18:19
                #7

                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

                "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                ~Napoleon Bonaparte

                On a crusade to banish setIndexWidget() from the holy land of Qt

                1 Reply Last reply
                2

                1/7

                9 Feb 2018, 20:44

                • Login

                • Login or register to search.
                1 out of 7
                • First post
                  1/7
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved