Reputation: 11
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.
Upvotes: 1
Views: 2072
Reputation: 36487
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?
Upvotes: 1
Reputation: 350
foundAccount should be a pointer. Account *foundAccount; This should make some of your errors go away.
Upvotes: 1
Reputation: 146930
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.
Upvotes: 0