在Qt中,当尝试分配引用时,我正在use of deleted function错误:
/home/niko/QT_snippets/QML1/users.cpp:16: error: use of deleted function 'User::User(const User&)' User user=users_map.value("email@domain.com"); ^ ^ /home/niko/QT_snippets/QML1/users.h:7: In file included from ../QML1/users.h:7:0, /home/niko/QT_snippets/QML1/users.cpp:1: from ../QML1/users.cpp:1: /home/niko/QT_snippets/QML1/user.h:6: 'User::User(const User&)' is implicitly deleted because the default definition would be ill-formed: class User : public QObject ^ /opt/Qt/5.7/gcc_64/include/QtCore/QObject:1: In file included from /opt/Qt/5.7/gcc_64/include/QtCore/QObject:1:0, /home/niko/QT_snippets/QML1/users.h:4: from ../QML1/users.h:4, /home/niko/QT_snippets/QML1/users.cpp:1: from ../QML1/users.cpp:1:在C中,我总是使用指针,我从来没有遇到任何问题,但正如我在C ++中看到的,每个人都使用引用。
如何在Qt中通过引用分配对象? 例如,在这一行中,我应该如何使user对象成为对users_map对象中的值的引用?
User user=users_map.value("email@domain.com");或者可能是以下?
User user=&users_map.value("email@domain.com");因为...上面的代码不能编译。 我需要在Users类的方法中使用它来访问users_map变量中的数据。
Users类声明为:
class Users : public QAbstractItemModel { Q_OBJECT enum UserRoles { EmailRole = Qt::UserRole + 1, NameRole, PasswordRole }; private: QMap<QString,User> users_map; public: explicit Users(QAbstractItemModel *parent = 0); Q_INVOKABLE QModelIndex index(int row, int column,const QModelIndex &parent = QModelIndex()) const; Q_INVOKABLE QModelIndex parent(const QModelIndex &child) const; Q_INVOKABLE int rowCount(const QModelIndex &parent = QModelIndex()) const; Q_INVOKABLE int columnCount(const QModelIndex &parent = QModelIndex()) const; Q_INVOKABLE QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const; QHash<int, QByteArray> roleNames() const; signals: public slots: };User类声明如下:
class User : public QObject { Q_OBJECT Q_PROPERTY(QString email READ get_email WRITE set_email NOTIFY emailChanged); Q_PROPERTY(QString name READ get_name WRITE set_name NOTIFY nameChanged); Q_PROPERTY(QString password READ get_password WRITE set_password NOTIFY passwordChanged); private: QString email; QString name; QString password; public: explicit User(QObject *parent = 0); QString get_email(); void set_email(QString data); QString get_name(); void set_name(QString data); QString get_password(); void set_password(QString data); signals: void emailChanged(); void nameChanged(); void passwordChanged(); public slots: };In Qt, when trying to assign a reference I am getting use of deleted function error:
/home/niko/QT_snippets/QML1/users.cpp:16: error: use of deleted function 'User::User(const User&)' User user=users_map.value("email@domain.com"); ^ ^ /home/niko/QT_snippets/QML1/users.h:7: In file included from ../QML1/users.h:7:0, /home/niko/QT_snippets/QML1/users.cpp:1: from ../QML1/users.cpp:1: /home/niko/QT_snippets/QML1/user.h:6: 'User::User(const User&)' is implicitly deleted because the default definition would be ill-formed: class User : public QObject ^ /opt/Qt/5.7/gcc_64/include/QtCore/QObject:1: In file included from /opt/Qt/5.7/gcc_64/include/QtCore/QObject:1:0, /home/niko/QT_snippets/QML1/users.h:4: from ../QML1/users.h:4, /home/niko/QT_snippets/QML1/users.cpp:1: from ../QML1/users.cpp:1:In C, I was always using pointers and I never had any problem, but as I see in C++ everybody uses references.
How should I assign an object by reference in Qt? For example, in this line how should I make the user object be a reference to a value in users_map object?
User user=users_map.value("email@domain.com");Or maybe the following?
User user=&users_map.value("email@domain.com");Because... the code above does not compile. I need to use it inside methods of Users class to access the data in users_map variable.
The Users class is declared as:
class Users : public QAbstractItemModel { Q_OBJECT enum UserRoles { EmailRole = Qt::UserRole + 1, NameRole, PasswordRole }; private: QMap<QString,User> users_map; public: explicit Users(QAbstractItemModel *parent = 0); Q_INVOKABLE QModelIndex index(int row, int column,const QModelIndex &parent = QModelIndex()) const; Q_INVOKABLE QModelIndex parent(const QModelIndex &child) const; Q_INVOKABLE int rowCount(const QModelIndex &parent = QModelIndex()) const; Q_INVOKABLE int columnCount(const QModelIndex &parent = QModelIndex()) const; Q_INVOKABLE QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const; QHash<int, QByteArray> roleNames() const; signals: public slots: };The User class is declared like this:
class User : public QObject { Q_OBJECT Q_PROPERTY(QString email READ get_email WRITE set_email NOTIFY emailChanged); Q_PROPERTY(QString name READ get_name WRITE set_name NOTIFY nameChanged); Q_PROPERTY(QString password READ get_password WRITE set_password NOTIFY passwordChanged); private: QString email; QString name; QString password; public: explicit User(QObject *parent = 0); QString get_email(); void set_email(QString data); QString get_name(); void set_name(QString data); QString get_password(); void set_password(QString data); signals: void emailChanged(); void nameChanged(); void passwordChanged(); public slots: };更多推荐
发布评论