我想制作一个QList of QwtPlotCurve,其原因是以后能够从我的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(QwtPlot曲线&)’无效QList::append(const &)与T= QwtPlotCurve的调用没有匹配函数
注:候选人如下:
注: void::追加(const&)和T= QwtPlotCurve
注:参数1从“QwtPlot曲线*”到“ConstQwtPlot曲线&”没有已知的转换。
注: void :append(const&)与T= QwtPlotCurve
注:参数1从“QwtPlot曲线*”到“const&”没有已知的转换。
..。
我说QwtPlotCurve应该是常数,但我不知道如何处理它。我也不知道在QList中存储曲线,然后(根据用户的要求)从图中删除曲线是否是正确的方法。
在斯杰沃纳回答后,我尝试了以下几点:
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指出的“隐式共享”,以摆脱“新”操作符。
发布于 2012-05-10 14:05:51
你有两个问题:
QList对象,然后尝试在堆上分配它--因为new返回指向您正在进行new活动的对象类型的指针,因此您实际上正在尝试执行以下操作:
QList = *QList
顺便提一下:您很少需要对new进行QList,因为Qt实现了对其所有容器类的隐式共享--简而言之,您可以自信地将所有Qt容器(和除了其他的课程)声明为堆栈对象,并在其他地方需要所包含的数据时按值传递-- Qt将处理所有内存效率和对象清理。
阅读 这 以获得更多信息.QList,并试图用指向对象的指针填充它。您需要决定是否希望QList包含数据的副本:
QList曲线;QwtPlotCurve curve1();QwtPlotCurve curve2();curves.append(curve1);curves.append(curve2);
或者是否要在堆上分配QwtPlotCurve,并将指向它们的指针存储在QList中:
QList曲线;curve1 =新QwtPlotCurve();curve2 =新QwtPlotCurve();curves.append(curve1);curves.append(curve2);https://stackoverflow.com/questions/10532368
复制相似问题