简体   繁体   中英

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

I'm trying to use a QList, a custom class (class Edge:public QGraphicsLineItem), but when I use.append or anything else I got compilation errors

 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);
    
    }

In my GraphBoard.h

QList<Edge> edges;

Maybe I should use *edges here but I get the same error...

I'm doing something wrong with custom class and QList. I already saw this topic Call to implicitly deleted copy constructor but I don't really know what I can take on this.

Here is Edge Defintion, I remove unused stuff:

#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();

};

You can't get copy of any object in c++ which class don't have a copy constructor. To use pointers is a solution, but writing copy constructor is easy and safe. For more information about copy constructors watch here.

You can't copy an object derived from QObject. So either don't derive from QObject or use pointers.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM