Solved use of deleted function 'Node::Node(const Node&)'
-
Hello!
I've come across this bug. Usually it has to do with trying to make a copy of a QObject, and maybe the connect does that, but I'm not sure...
I've cut down the code as much as possible so it still replicates the error//net.h #ifndef NET_H #define NET_H #include <QObject> #include "node.h" class Net : public QObject { Q_OBJECT public: explicit Net(QObject *parent = nullptr); void foo(); signals: QString requestNodeInfo(); private: QVector<Node> node; }; #endif // NET_H
//node.h #ifndef NODE_H #define NODE_H #include <QObject> class Net; class Node : public QObject { Q_OBJECT public: explicit Node(Net *parentNet = nullptr); public slots: QString provideNodeInfo(); }; #endif // NODE_H
//main.cpp #include <QCoreApplication> int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); return a.exec(); }
//net.cpp #include "net.h" Net::Net(QObject *parent) : QObject(parent) { } void Net::foo() { for (int i=0; i<node.size(); i++) { connect(this,&Net::requestNodeInfo,&node[i],&Node::provideNodeInfo); } }
//node.cpp #include "node.h" #include "net.h" Node::Node(Net *parentNet) : QObject(parentNet) { } QString Node::provideNodeInfo() { }
The error messages are:
use of deleted function 'Node::Node(const Node&)'
'QObject::QObject(const &QObject) is private within this context -
Hi and welcome to the forums :)
if this line is correct
QVector<Node> node;
that is the reason.
It has to be
QVector<Node *> node;
or else it will try to copy the Node into the list. (which is not allowed)
So the template type must be * (pointer to Node) -
Hi and welcome to the forums :)
if this line is correct
QVector<Node> node;
that is the reason.
It has to be
QVector<Node *> node;
or else it will try to copy the Node into the list. (which is not allowed)
So the template type must be * (pointer to Node) -
Thank you, had a blind spot there haha!
-
@fT3g0
:) we all know such situations.