0

How can I solve the following, ie, converting const QVector<QVector<qreal>> to QVector<QVector<qreal>>?

I tried a few steps but didn't help:

QVector<QVector<qreal>> points = const_cast<QVector<QVector<qreal>>>(abc.points);

abc.points is a struct element of type QVector<QVector<qreal>>, which I'm trying to extract from a QDataStream:

QDataStream& operator >> (QDataStream& in, const CustomPointCloud& abc)
{
    quint32 pointsCount = quint32(abc.pointsCount);
    QVector<QVector<qreal>> points =
        const_cast<QVector<QVector<qreal>>>(abc.points);
    in >> pointsCount >> points;
    return in;
}
pschill
  • 5,055
  • 1
  • 21
  • 42
Sayan Bera
  • 135
  • 2
  • 16

2 Answers2

2

<< takes by const, because it does not modify the parameter, whereas the whole point of >> is to modify the parameter.

You should change your function definition. You are reading data from the stream into a local object which ceases to exist at the end of the function.

QDataStream& operator >> (QDataStream& in, CustomPointCloud& abc)
{
    quint32 pointsCount;
    in >> pointsCount;
    in >> abc.points;
    return in;
}

I would also suggest that you don't need the count of points to extract the stream, the underlying QDataStream& >> (QDataStream&, QVector<T>&) template deals with that. The pair of operators would then be

QDataStream& operator >> (QDataStream& in, CustomPointCloud& abc)
{
    return in >> abc.points;
}

QDataStream& operator << (QDataStream& out, const CustomPointCloud& abc)
{
    return out << abc.points;
}
Caleth
  • 52,200
  • 2
  • 44
  • 75
0

Got it done by QVector<QVector<qreal>> points(abc.points);

Please suggest if there are other approaches.

Sayan Bera
  • 135
  • 2
  • 16