C++ QList继承自定义方法问题

发布于 2024-09-18 01:20:57 字数 1452 浏览 6 评论 0原文

我正在通过继承创建一个名为 AccountList 的 Account* 类型的自定义 QList。

我的 AccountList 接口声明如下:

class Client
{
    public:
        Client(QString firstName, QString lastName, QString address1, QString address2, QString postalCode);
        QString toString();

    private:
        QString m_FirstName;
        QString m_LastName;
        QString m_Address1;
        QString m_Address2;
        QString m_PostalCode;
};

class Account
{
    public:
        Account(unsigned acctNum, double balance, const Client owner);
        unsigned getAcctNum();
        double getBalance();
        Client getOwner();
        virtual ~Account();

    private:
        unsigned m_AcctNum;
        double m_Balance;
        Client m_Owner;
};

class AccountList : public QList<Account*>
{
    public:
        QString toString() const;
        Account* findAccount(unsigned accNum) const;
        bool addAccount(const Account* acc) const;
        bool removeAccount(unsigned accNum) const;
};

我在实现 AccountList 时遇到问题,例如 findAccount 方法。

Account* AccountList::findAccount(unsigned accNum) const
{
    Account foundAccount;
    foreach(Account* acc, this)
    {
        if (acc->getAcctNum() == accNum)
        {
            foundAccount = acc;
            break;
        }
    }
    return foundAccount;
}

希望上述方法能让您了解我想要实现的目标。看起来非常简单直接,但我无法让它发挥作用。 Qt Creator 编译器在编译时给我带来了各种奇怪的错误。

任何帮助将不胜感激。

I am creating a custom QList of type Account* called AccountList through inheritance.

My interface declaration for AccountList is as follows:

class Client
{
    public:
        Client(QString firstName, QString lastName, QString address1, QString address2, QString postalCode);
        QString toString();

    private:
        QString m_FirstName;
        QString m_LastName;
        QString m_Address1;
        QString m_Address2;
        QString m_PostalCode;
};

class Account
{
    public:
        Account(unsigned acctNum, double balance, const Client owner);
        unsigned getAcctNum();
        double getBalance();
        Client getOwner();
        virtual ~Account();

    private:
        unsigned m_AcctNum;
        double m_Balance;
        Client m_Owner;
};

class AccountList : public QList<Account*>
{
    public:
        QString toString() const;
        Account* findAccount(unsigned accNum) const;
        bool addAccount(const Account* acc) const;
        bool removeAccount(unsigned accNum) const;
};

I am having a problem with implementation of AccountList, e.g. the findAccount method.

Account* AccountList::findAccount(unsigned accNum) const
{
    Account foundAccount;
    foreach(Account* acc, this)
    {
        if (acc->getAcctNum() == accNum)
        {
            foundAccount = acc;
            break;
        }
    }
    return foundAccount;
}

Hope the above method gives you an idea of what i am trying to accomplish. Seems pretty simple and straigh forward, but i can't get it to work. Qt Creator compiler gives me all sorts of strange errors on compiling.

Any help would be appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

放肆 2024-09-25 01:20:57

foundAccount 应该是一个指针。
账户*找到账户;
这应该会让你的一些错误消失。

foundAccount should be a pointer.
Account *foundAccount;
This should make some of your errors go away.

凉城已无爱 2024-09-25 01:20:57

没有准备好 Qt 安装来测试它,但我确信您忘记了派生类 (AccountList) 中的宏 Q_OBJECT

class myClass : public SomeQtClass
{
    Q_OBJECT
    public:
    // ...
}

另请参阅此问题:Qt 问题:做什么Q_OBJECT宏做什么?

Don't have a Qt installation ready to test it, but I'm rather sure you forgot the macro Q_OBJECT inside your derived class (AccountList):

class myClass : public SomeQtClass
{
    Q_OBJECT
    public:
    // ...
}

See also this question: Qt question: Whad does the Q_OBJECT macro do?

陪你到最终 2024-09-25 01:20:57

除了在 C++0x 中之外,foreach 不是有效的 C++ 构造,即使如此,它也不采用该格式。使用积分循环或 std::for_each。此外,您真的、真的不应该拥有帐户*,您应该使用某种形式的智能指针。最后,你声明了你的方法const,这使得你继承的QList(为什么不只是将它作为成员变量?)const,这使得你想要返回const的Account* - 但你试图只返回一个非常量。哎呀。

foreach is not a valid C++ construct except in C++0x, and even then it doesn't take on that format. Use an integral loop or std::for_each. In addition, you really, really shouldn't have an Account*, you should use some form of smart pointer. Finally, you declared your method const, which makes the QList you inherited from (why not just have it as a member variable?) const, which makes the Account* you want to return const- but you tried to just return a non-const. Whoops.

不再让梦枯萎 2024-09-25 01:20:57

尝试使用这个:

foreach ( Account* account, *this )
{
}

Try using this:

foreach ( Account* account, *this )
{
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文