convert from 'const QVector<QVector<qreal>>' to 'QVector<QVector<qreal>>'

255 views Asked by At

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;
}
2

There are 2 answers

0
Sayan Bera On BEST ANSWER

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

Please suggest if there are other approaches.

1
Caleth On

<< 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;
}