简体   繁体   中英

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

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

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

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

Please suggest if there are other approaches.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM