c++ - 无法使用自定义类创建 QList

标签 c++ qt qlist

我正在尝试创建自定义类对象的 QList,但出现错误:

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

我的代码(用户 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

Qt 给我错误列表:

...\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'

阅读标题:

#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

这本书被评论了,因为它也给出了错误...

最佳答案

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

看起来 Read 在 user.h 中是未知的。 也许 date.h 或 book.h 也包括 user.h? (循环引用)

使用 prototype classes可以帮助防止这种情况。

关于c++ - 无法使用自定义类创建 QList,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30827624/

相关文章:

c++ - 如何在 Matlab 中分析 MEX 函数

c++ - 更好地理解LRU算法

c++ - 取消引用 nullptr 时编译器不会发出警告

c++ - 加载从信号槽接收到的 QStringList 值

qt - 删除 QTableView 中的多行

c++ - 常量类成员、赋值运算符和 QList

c++ - SDL程序可以自己运行,不能通过命令行运行

c++ - 我可以使用 Visual C++ 开发跨平台桌面应用程序吗?

qt - 无法在项目文件中包含OpenBR库文件

qt - 为什么 Qt 的容器类不允许可 move 、不可复制的元素类型?