在Qt,試圖分配一個參考,當我收到use of deleted function
錯誤:如何在Qt for QObject中通過引用進行賦值?
/home/niko/QT_snippets/QML1/users.cpp:16: error: use of deleted function 'User::User(const User&)'
User user=users_map.value("[email protected]");
^
^
/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 protected]");
或者可能是以下內容?
User user=&users_map.value("[email protected]");
因爲...上面的代碼不能編譯。我需要在Users
類的方法內使用它來訪問users_map
變量中的數據。
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
類聲明如下:
爲Users
類被聲明
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:
};
'用戶和用戶= users_map.value( 「[email protected]」);' –
*「試圖分配時一個參考「* - 你從來沒有嘗試過分配參考。而且你用QT對象和你用任何其他對象做同樣的事情。在嘗試使用像QT這樣的高級庫之前,我建議使用C++初學者的書。 –
@IgorTandetnik沒有實現'QMap :: value'返回一個引用,所以你的代碼仍然是錯誤的 – wasthishelpful