Results 1 to 4 of 4

Thread: Serialization two vectors that are in a structure

  1. #1
    Join Date
    Jun 2018
    Posts
    31
    Thanks
    8
    Qt products
    Qt5
    Platforms
    MacOS X Windows

    Default Serialization two vectors that are in a structure

    Hi,
    How do I overload the << >> functions to be able to serialize the vectors?
    The struct is the next one:

    struct realDataValue{
    QVector<double> xValueVector;
    QVector<double> yValueVector;
    bool operator==(const realDataValue &other) const
    {
    return xValueVector==other.xValueVector && yValueVector==other.yValueVector;
    }
    };

    Thanks a lot.

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    4,307
    Thanks
    244
    Thanked 678 Times in 668 Posts
    Qt products
    Qt5
    Platforms
    Windows Android

    Default Re: Serialization two vectors that are in a structure

    QVector<T> already has an implementation for serialization to a QDataStream via non-menber functions operator>> and operator<<. Do you need to serialize to some different kind of stream?
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  3. #3
    Join Date
    Jun 2018
    Posts
    31
    Thanks
    8
    Qt products
    Qt5
    Platforms
    MacOS X Windows

    Default Re: Serialization two vectors that are in a structure

    Thanks for answering
    I need serialization of variables type QString and double.
    I have already used the following code
    inline QDataStream & operator << (QDataStream & outStream, const realDataValue & mRealDataValue)
    {
    return outStream << mRealDataValue.xValueVector << mRealDataValue.yValueVector;
    }

    but when reading a QVector <QString> or QVector <double> of a stream it sends me an error, "invalid operands to binary expression (QDataStream and const QVector <QString>)

    inline QDataStream & operator >> (QDataStream & outStream, const realDataValue & mRealDataValue)
    {
    return outStream >> mRealDataValue.xValueVector >> mRealDataValue.yValueVector;
    }

    thanks

  4. #4
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    4,307
    Thanks
    244
    Thanked 678 Times in 668 Posts
    Qt products
    Qt5
    Platforms
    Windows Android

    Default Re: Serialization two vectors that are in a structure

    but when reading a QVector <QString> or QVector <double> of a stream it sends me an error
    because in your operator>> method, you have declared the realDataValue argument as "const". You can't modify a const reference.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  5. The following user says thank you to d_stranz for this useful post:

    Eduardo Huerta (19th December 2019)

Similar Threads

  1. Sharing vectors between classes
    By dubstar_04 in forum Newbie
    Replies: 2
    Last Post: 7th March 2014, 10:49
  2. Replies: 12
    Last Post: 26th June 2011, 12:26
  3. QDataStream reading to C++ vectors
    By pdoria in forum Qt Programming
    Replies: 6
    Last Post: 24th July 2009, 21:34
  4. position vectors
    By KaKa in forum Newbie
    Replies: 6
    Last Post: 18th March 2007, 17:06
  5. Array of Vectors
    By ToddAtWSU in forum General Programming
    Replies: 3
    Last Post: 12th April 2006, 15:44

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.