gpt4 book ai didi

c++ - 'QObject& QObject::operator=(const QObject&)' 是私有(private)的

转载 作者:太空宇宙 更新时间:2023-11-04 15:19:04 28 4
gpt4 key购买 nike

将一个项目从 Qt4 迁移到 Qt5 我得到了这个错误,我已经研究过,显然你不能从 QObject 创建一个派生类的复制构造函数(这太不可思议了,因为这段代码不是我的,它应该在以前的版本中编译).复制构造函数并没有真正复制任何 QObject 值,而且我不知道 QList 有什么问题。

In file included from ..\..\..\..\..\Qt5\5.2.1\mingw48_32\include/QtGui/qwindowdefs.h:45:0,
from ..\..\..\..\..\Qt5\5.2.1\mingw48_32\include\QtWidgets/qwidget.h:45,
from ..\..\..\..\..\Qt5\5.2.1\mingw48_32\include\QtWidgets/QWidget:1,
from ..\marssies\wavingwidget.h:4,
from ..\marssies\wavingwidget.cpp:1:
..\marssies\graphicsprimitive.h: In instantiation of 'void QList<T>::node_construct(QList<T>::Node*, const T&) [with T = SIZArrow]':
..\..\..\..\..\Qt5\5.2.1\mingw48_32\include/QtCore/qlist.h:515:32: required from 'void QList<T>::append(const T&) [with T = SIZArrow]'
..\..\..\..\..\Qt5\5.2.1\mingw48_32\include/QtCore/qlist.h:301:49: required from 'void QList<T>::push_back(const T&) [with T = SIZArrow]'
..\marssies\wavingwidget.cpp:345:23: required from here
In file included from ..\..\..\..\..\Qt5\5.2.1\mingw48_32\include/QtGui/qwindowdefs.h:45:0,
from ..\..\..\..\..\Qt5\5.2.1\mingw48_32\include\QtWidgets/qwidget.h:45,
from ..\..\..\..\..\Qt5\5.2.1\mingw48_32\include\QtWidgets/QWidget:1,
from ..\marssies\wavingwidget.h:4,
from ..\marssies\wavingwidget.cpp:1:
..\marssies\graphicsprimitive.h: In instantiation of 'void QList<T>::node_construct(QList<T>::Node*, const T&) [with T = SIZPolygon]':
..\..\..\..\..\Qt5\5.2.1\mingw48_32\include/QtCore/qlist.h:515:32: required from 'void QList<T>::append(const T&) [with T = SIZPolygon]'
..\..\..\..\..\Qt5\5.2.1\mingw48_32\include/QtCore/qlist.h:301:49: required from 'void QList<T>::push_back(const T&) [with T = SIZPolygon]'
..\marssies\wavingwidget.cpp:330:24: required from here
..\..\..\..\..\Qt5\5.2.1\mingw48_32\include/QtCore/qglobal.h:981:12: error: 'QObject& QObject::operator=(const QObject&)' is private
Class &operator=(const Class &) Q_DECL_EQ_DELETE;
^
..\..\..\..\..\Qt5\5.2.1\mingw48_32\include/QtCore/qobject.h:465:5: note: in expansion of macro 'Q_DISABLE_COPY'
Q_DISABLE_COPY(QObject)
^
In file included from ..\marssies\wavingwidget.h:10:0,
from ..\marssies\wavingwidget.cpp:1:
..\marssies\graphicsprimitive.h:50:7: error: within this context
class GraphicsPrimitive : public QObject {
^
In file included from ..\marssies\wavingwidget.h:13:0,
from ..\marssies\wavingwidget.cpp:1:
..\marssies\polygon.h:6:7: note: synthesized method 'GraphicsPrimitive& GraphicsPrimitive::operator=(const GraphicsPrimitive&)' first required here
class SIZPolygon : public GraphicsPrimitive {
^
In file included from ..\..\..\..\..\Qt5\5.2.1\mingw48_32\include/QtCore/qobject.h:51:0,
from ..\..\..\..\..\Qt5\5.2.1\mingw48_32\include\QtWidgets/qwidget.h:46,
from ..\..\..\..\..\Qt5\5.2.1\mingw48_32\include\QtWidgets/QWidget:1,
from ..\marssies\wavingwidget.h:4,
from ..\marssies\wavingwidget.cpp:1:
..\..\..\..\..\Qt5\5.2.1\mingw48_32\include/QtCore/qlist.h:372:35: note: synthesized method 'SIZPolygon& SIZPolygon::operator=(const SIZPolygon&)' first required here
else *reinterpret_cast<T*>(n) = t;
^

部分wavingwidget.cpp

SIZPolygon d;
/* ...more code...*/
d.setData(TRIANGLE_VERTEX_FIRST, w1Data.pointNumber);
d.setData(TRIANGLE_VERTEX_SECOND, w2Data.pointNumber);
d.setData(TRIANGLE_VERTEX_THIRD, w3Data.pointNumber);
d.setData(POLYGON_WAVEHEIGHT, waveAltVal);

polygons.push_back(d); //wavingwidget.cpp:330:24

double waveComputedDir = (w1Data.waveDirection + w2Data.waveDirection + w3Data.waveDirection) / 3.0;

部分graphicsprimitive.h

class GraphicsPrimitive : public QObject { //graphicsprimitive.h:50:7

Q_OBJECT
friend class PainterBar;

public:
GraphicsPrimitive(void);
GraphicsPrimitive(const GraphicsPrimitive&);
~GraphicsPrimitive(void);

部分polygon.h

class SIZPolygon : public GraphicsPrimitive { //polygon.h:6:7

public:
SIZPolygon();
~SIZPolygon(void);
void draw(QPainter*);
void addPoint(double, double, int);
bool containsScreen(int, int);
bool containsGeo(double, double);
QList<QPair<int, int> > getCenterPoints();
QPair<double, double> getGeoCenterPoint();
bool isOverNode(int, int, int*);
void movePoint(int, int, int);
void move(int, int);
void toXML(QDomElement*, QDomDocument*);
int containsSegment(int, int);

private:
void countCOFMA(double*, double, double, double, double);
};

如您所见,我通过常量引用传递 QObject,所以实际上我不确定代码有什么问题,可能是 QList 的问题,但我不明白是怎么回事。我见过类似的问题,但没有找到任何可以帮助我的东西。
我会很高兴知道哪个部分失败/要寻找什么,这样我就可以更好地寻找解决方案编辑

我按照@ratchetfreak 告诉我的做了,并设法解决了一些错误,但它仍然说它是私有(private)的:

..\..\..\..\..\Qt5\5.2.1\mingw48_32\include/QtCore/qglobal.h:981:12: error: 'QObject& QObject::operator=(const QObject&)' is private
Class &operator=(const Class &) Q_DECL_EQ_DELETE;
^
..\..\..\..\..\Qt5\5.2.1\mingw48_32\include/QtCore/qobject.h:465:5: note: in expansion of macro 'Q_DISABLE_COPY'
Q_DISABLE_COPY(QObject)
^
In file included from ..\marssies\wavingwidget.h:10:0,
from ..\marssies\wavingwidget.cpp:1:
..\marssies\graphicsprimitive.h:50:7: error: within this context
class GraphicsPrimitive : public QObject {
^
In file included from ..\marssies\wavingwidget.h:11:0,
from ..\marssies\wavingwidget.cpp:1:
..\marssies\arrow.h:7:7: note: synthesized method 'GraphicsPrimitive& GraphicsPrimitive::operator=(const GraphicsPrimitive&)' first required here
class SIZArrow : public GraphicsPrimitive
^
In file included from ..\..\..\..\..\Qt5\5.2.1\mingw48_32\include/QtCore/qobject.h:51:0,
from ..\..\..\..\..\Qt5\5.2.1\mingw48_32\include\QtWidgets/qwidget.h:46,
from ..\..\..\..\..\Qt5\5.2.1\mingw48_32\include\QtWidgets/QWidget:1,
from ..\marssies\wavingwidget.h:4,
from ..\marssies\wavingwidget.cpp:1:
..\..\..\..\..\Qt5\5.2.1\mingw48_32\include/QtCore/qlist.h:372:35: note: synthesized method 'SIZArrow& SIZArrow::operator=(const SIZArrow&)' first required here
else *reinterpret_cast<T*>(n) = t;

最佳答案

QObject 不可复制,只能通过指针引用;所以QList<SIZPolygon>的声明(正如我在您的错误消息中看到的那样)迟早会导致问题。

如果不是真正需要的话,要么从 QObject 中移除对 GraphicsPrimitive 的继承

或将其更改为 QList<SIZPolygon*>并根据需要重新设置

SIZPolygon* d = new SIZPolygon();
d->setParent(this);
//convention is to add a QObject parent=0 to the constructor but this works as well

当你需要删除它时,你调用d->deleteLater()在上面。

关于c++ - 'QObject& QObject::operator=(const QObject&)' 是私有(private)的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23628890/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com