C++ QList继承自定义方法问题
我正在通过继承创建一个名为 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
foundAccount 应该是一个指针。
账户*找到账户;
这应该会让你的一些错误消失。
foundAccount should be a pointer.
Account *foundAccount;
This should make some of your errors go away.
没有准备好 Qt 安装来测试它,但我确信您忘记了派生类 (AccountList) 中的宏
Q_OBJECT
:另请参阅此问题: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):See also this question: Qt question: Whad does the Q_OBJECT macro do?
除了在 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.
尝试使用这个:
Try using this: