QList<customClass> : 不能使用 QList 因为 "implicitly-deleted copy"

QList<customClass> : can't use QList because "implicitly-deleted copy"

我正在尝试使用 QList,自定义 class (class Edge:public QGraphicsLineItem),但是当我使用 .append 或其他任何东西时,我遇到了编译错误

 call to implicitly-deleted copy constructor of 'Edge'
                 current->v = new T(*reinterpret_cast<T*>(src->v));
                                  ^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Graphboard.cpp

  void GraphBoard::createEdge()
    {
       
       Edge newEdge(item1,item2);
       edges.append(newEdge);
    
    }

在我的GraphBoard.h

QList<Edge> edges;

也许我应该在这里使用 *edges 但我得到了同样的错误...

我在自定义 class 和 QList 上做错了。 我已经看过这个话题 Call to implicitly deleted copy constructor 但我真的不知道我能对此做些什么。

这是边缘定义,我删除了未使用的东西:

#include <QObject>
#include <QWidget>
#include <QApplication>
#include <QGraphicsScene>
#include <QGraphicsView>
#include <QGraphicsItem>

class Edge:public QGraphicsLineItem
{

    QGraphicsEllipseItem * state1;
    QGraphicsEllipseItem * state2;

public:
    enum { Type = UserType + 2 };
    int type() const override;
    Edge(QGraphicsEllipseItem * s1, QGraphicsEllipseItem* s2);

     QList<QPointF> centerPoints();

};

您不能复制从 QObject 派生的对象。所以要么不要从 QObject 派生,要么使用指针。

您无法获取任何 class 没有复制构造函数的 c++ 对象的副本。 使用指针是一种解决方案,但是编写复制构造函数既简单又安全。 有关复制构造函数的更多信息 watch here.