Can't create QList with custom class

850 views Asked by At

I'm trying to create QList of custom class objects, but I got error:

error: C2923: 'QList' : 'Read' is not a valid template type argument for parameter 'T'

My code (User header):

#ifndef USER_H
#define USER_H

#include <QString>
#include <QList>
#include "read.h"

class User {
protected:
    int id;
    QString username;
    QString password;
    QList<Read> readBooks;
    bool accountDeleted;
    bool admin;
public:
    User();
    User(int id, QString username, QString password,
         QList<Read> readBooks, bool accountDeleted, bool admin);
    ~User();
    const int getId();
    void setId(int id);
    const QString getUsername();
    void setUsername(QString username);
    const QString getPassword();
    void setPassword(QString password);
    const QList<Read> getReadBooks();
    void setReadBooks(QList<Read> readBooks);
    const bool isAccountDeleted();
    void setAccountDeleted(bool accountDeleted);
    const bool isAdmin();
    void setAdmin(bool admin);
};

QDataStream &operator<<(QDataStream &out, const User &user);
QDataStream &operator>>(QDataStream &in, User &user);

#endif // USER_H

and Qt gives me error list:

...\user.h(13) : error C2065: 'Read' : undeclared identifier
...\user.h(13) : error C2923: 'QList' : 'Read' is not a valid template type argument for parameter 'T'
...\user.h(18) : error C2065: 'Read' : undeclared identifier
...\user.h(18) : error C2923: 'QList' : 'Read' is not a valid template type argument for parameter 'T'
...\user.h(27) : error C2065: 'Read' : undeclared identifier
...\user.h(27) : error C2923: 'QList' : 'Read' is not a valid template type argument for parameter 'T'
...\user.h(28) : error C2065: 'Read' : undeclared identifier
...\user.h(28) : error C2923: 'QList' : 'Read' is not a valid template type argument for parameter 'T'

Read header:

#ifndef READ_H
#define READ_H

#include <QDataStream>
#include "book.h"
#include "date.h"

class Read
{
protected:
    //Book book;
    Date addDate;
    Date readDate;
    bool stillReading;
public:
    Read();
    ~Read();
    //Read(Book book, Date addDate, Date readDate, bool stillReading);
    //Book getBook();
    //void setBook(Book book);
    Date getAddDate();
    void setAddDate(Date addDate);
    Date getReadDate();
    void setReadDate(Date readDate);
    bool isStillReading();
    void setStillReading(bool stillReading);
};

QDataStream &operator<<(QDataStream &out, const Read &read);
QDataStream &operator>>(QDataStream &in, Read &read);

#endif // READ_H

Book is commented, because it gives errors too...

2

There are 2 answers

1
550 On BEST ANSWER

...\user.h(13) : error C2065: 'Read' : undeclared identifier

Looks like Read is not known in user.h. Maybe date.h or book.h are including user.h as well? (circular references)

Using prototype classes can help to prevent this.

1
cmannett85 On

From the docs:

QList's value type must be an assignable data type.

So you need a copy-constructor.