PDA

View Full Version : Serialization two vectors that are in a structure



Eduardo Huerta
17th December 2019, 02:41
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.

d_stranz
17th December 2019, 04:46
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?

Eduardo Huerta
17th December 2019, 17:32
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

d_stranz
17th December 2019, 17:47
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.