gpt4 book ai didi

c++ - 无法在 QList 中添加我的类(class)

转载 作者:搜寻专家 更新时间:2023-10-31 00:11:18 25 4
gpt4 key购买 nike

我有从 QObject 传递的自定义类:

class Client : public QObject
{
Q_OBJECT
friend class Server;
public:
Client(QTcpSocket *socket, QObject *parent = 0);
private:
QTcpSocket *mSocket;
};

并尝试将其添加到我的列表(QList)中

auto socket = mServer->nextPendingConnection();
Client client(socket);
mClients.append(client);

接下来输出:

/usr/include/x86_64-linux-gnu/qt5/QtCore/qlist.h:521: required from 'void QList::append(const T&) [with T = Client]'
...
/usr/include/x86_64-linux-gnu/qt5/QtCore/qlist.h:372: error: use of deleted function 'Client::Client(const Client&)' if (QTypeInfo::isLarge || QTypeInfo::isStatic) n->v = new T(t);

如何将我的对象正确添加到列表中?

最佳答案

QObject具有私有(private)复制构造函数和赋值运算符。您应该存储智能指针列表,而不是对象。

你不应该为派生自 QObject 的类定义复制构造函数。

Instances of subclasses of QObject should not be thought of as valuesthat can be copied or assigned, but as unique identities. This meansthat when you create your own subclass of QObject (director orindirect), you should not give it a copy constructor or an assignmentoperator.

No Copy Constructor or Assignment Operator

QObject has neither a copy constructor nor an assignment operator.This is by design. Actually, they are declared, but in a privatesection with the macro Q_DISABLE_COPY(). In fact, all Qt classesderived from QObject (direct or indirect) use this macro to declaretheir copy constructor and assignment operator to be private.

关于c++ - 无法在 QList 中添加我的类(class),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34461898/

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