views:

42

answers:

2

Hi, i am new to C++

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.

A: 

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.

DeadMG
In the QT C++ framework, foreach is a valid construct. I even tried this without using foreach and opted for iterators instead, but same difference. The only reason QList is a list of Account* pointers is because thats what my assignment has requested.What i really need to know is how to iterate through this list so that i may return the Account object from the list based on the account number.
Sameer Shariff
+1  A: 

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

Ganesh