【发布时间】:2012-05-10 10:44:33
【问题描述】:
我想制作一个 QwtPlotCurve 的 QList。这样做的原因是以后能够从我的 QwtPlot 中删除它们。 我有以下代码:
QList<QwtPlotCurve> myList = new QList<QwtPlotCurve>;
QwtPlotCurve* curve1 = new QwtPlotCurve();
QwtPlotCurve* curve2 = new QwtPlotCurve();
curves->append(curve1);
curves->append(curve2);
代码无法编译,编译器输出:
错误:请求从“QList”转换为非标量类型“QList”
错误:没有匹配函数调用 'QList::append(QwtPlotCurve
注意:候选人是:
注意: void QList::append(const T&) [with T = QwtPlotCurve]
注意:没有已知的参数 1 从 'QwtPlotCurve*' 到 'const QwtPlotCurve&' 的转换
注意: void QList::append(const QList&) [with T = QwtPlotCurve]
注意:没有已知的参数 1 从 'QwtPlotCurve*' 到 'const QList&' 的转换
...
我说 QwtPlotCurve 应该是恒定的,但我不知道如何处理它。 我也不知道将曲线存储在 QList 中然后(根据用户需求)从图中删除是否是正确的方法。
在 sjwarner 的回答之后,我尝试了以下方法:
QList<QwtPlotCurve*> curves;
QwtPlotCurve* curve1 = new QwtPlotCurve();
QwtPlotCurve* curve2 = new QwtPlotCurve();
curves->append(curve1);
curves->append(curve2);
我收到以下错误:
错误:“->”的基本操作数具有非指针类型“QList” 错误:“->”的基本操作数具有非指针类型“QList”
我通过以下方式理解此错误: 曲线是一个 QList,它应该是一个指向 QList 的指针。
如果我尝试:
QList<QwtPlotCurve*>* curves = new QList<QwtPlotCurve*>;
QwtPlotCurve* curve1 = new QwtPlotCurve();
QwtPlotCurve* curve2 = new QwtPlotCurve();
curves->append(curve1);
curves->append(curve2);
它工作正常。 我要看看 sjwarner 指出的“隐式共享”,以摆脱“新”运算符。
【问题讨论】:
-
您在堆栈上声明 QList
,但在堆上分配它... -
这是我见过的 SO 标题中 Q 最多的。