[英]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>>
? 我该如何解决以下问题, const QVector<QVector<qreal>>
为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
: abc.points是QVector<QVector<qreal>>
类型的结构元素,我正尝试从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. <<
由const
,因为它不会修改参数,而>>
的全部要点就是修改参数。
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. 我还建议您不需要点数即可提取流,底层的QDataStream& >> (QDataStream&, QVector<T>&)
模板可以处理该点。 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);
通过QVector<QVector<qreal>> points(abc.points);
Please suggest if there are other approaches. 请提出是否还有其他方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.