【问题标题】:C++ Erasing element from a vector of pointersC ++从指针向量中擦除元素
【发布时间】:2014-11-23 20:37:56
【问题描述】:

我将指针向量用于继承目的,并且我的所有行为都应有尽有,但是,我在从向量中删除对象时遇到了问题。

我创建了一个包含所有源文件的 Github gist,以及一个易于编译和运行的 Makefile:

https://gist.github.com/anonymous/7c689940992f5986f51e

问题主要出在这里:

我有一个这样的 Bank 类(注意 Account Database 结构,它是 Account* 的封装向量):

class Bank
{
private:
    Database<Account> m_acctDb;
    Database<User> m_userDb;
    int m_accountCounter;
    int m_userCounter;
public:
    Bank ();
    ~Bank ();

    // Accessor Methods.
    int getAccountCounter () const;
    int getUserCounter () const;
    int getNumAccounts () const;
    int getNumUsers () const;
    Database<Account> getAccountDatabase () const;
    Database<User> getUserDatabase () const;
    User *getUser (int userId);
    Account *getAccount (int accountId);
    std::vector<ChequingAccount> getChequingAccounts () const;
    std::vector<SavingsAccount> getSavingsAccounts () const;
    std::vector<Manager> getManagers () const;
    std::vector<Customer> getCustomers () const;
    std::vector<Maintenance> getMaintenance () const;

    // Mutator Methods.
    void addChequing (int userId, double balance = 0, std::string name =
            "");
    void addSavings (int userId, double balance = 0,
            std::string name = "");
    void addCustomer (std::string firstName, std::string lastName,
            std::string password, std::string username);
    void addManager (std::string firstName, std::string lastName,
            std::string password, std::string username);
    void addMaintenance (std::string firstName, std::string lastName,
            std::string password, std::string username);
    void deleteAccount (int accountId);
    void deleteUser (int userId);
};

我有一个这样的模板数据库类,它创建指针向量:

template<class T>
    class Database
    {
        protected:
            std::vector<T*> m_database;
        public:
            Database ();
            virtual ~Database ();
            std::vector<T*> getDatabase () const;
            void add (T *Object);
            bool del (int id);
    };

在我的银行中,我添加了 Account 对象,它们的定义如下:

class Account
{
    protected:
        int m_id, m_userId, m_type;
        double m_balance;
        std::string m_name;
        std::vector<std::string> m_history;
public:
    Account (int id, int userId, double balance = 0,
            std::string name = "");
    virtual ~Account ();

    // Accessor Methods.
    int getId () const;
    int getUserId () const;
    int getType () const;
    double getBalance () const;
    std::string getName () const;
    std::string getDetails () const;
    std::vector<std::string> getHistory () const;

    // Mutator Methods.
    void setName (std::string name);
    void depositFunds (double amount);
    virtual bool withdrawFunds (double amount);
};

/*
 * Chequing account type
 */
class ChequingAccount : public Account
{
    public:
        ChequingAccount (int id, int userId, double balance = 0,
                std::string name = "") :
                Account(id, userId, balance, name)
        {
            // Chequing account type.
            m_type = CHEQ;

            // If no name was given.
            if (name == "")
            {
                // Give account a generic name.
                m_name = "Chequing Account #" + std::to_string(id);
            }
            else
            {
                m_name = name;
            }
        }

        ~ChequingAccount ()
        {
        }

        virtual bool withdrawFunds (double amount);
};

/*
 * Savings account type
 */
class SavingsAccount : public Account
{
    public:
        SavingsAccount (int id, int userId, double balance, std::string name) :
                Account(id, userId, balance, name)
        {
            // Savings account type.
            m_type = SAVE;

            // If no name was given.
            if (name == "")
            {
                // Give account a generic name.
                m_name = "Savings Account #" + std::to_string(id);
            }
            else
            {
                m_name = name;
            }
        }

        ~SavingsAccount ()
        {
        }
};

我正在将 Account 对象(主要是从基本 Account 类派生的类的 Chequing/Savings Accounts )添加到 Bank,如下所示:

/*
 * Adds a new chequing account to the bank database.
 */
void Bank::addChequing (int userId, double balance, std::string name)
{
    m_acctDb.add(new ChequingAccount(++m_accountCounter, userId, balance, name));
}

/*
 * Adds a new savings account to the bank database.
 */
void Bank::addSavings (int userId, double balance, std::string name)
{
    m_acctDb.add(new SavingsAccount(++m_accountCounter, userId, balance, name));
}

这一切都正常工作,我能够从数据库中提取对象并按照我喜欢的方式进行操作。问题在于删除,它的定义如下:

/*
 * Deletes the specified account from the bank database.
 */
void Bank::deleteAccount (int accountId)
{
    std::vector<Account*> db = m_acctDb.getDatabase();
    std::vector<Account*>::iterator it = db.begin();
    cout << "Searching for account " << accountId << endl;
    while (it != db.end())
    {
        if ((*it)->getId() == accountId)
        {
            cout << "Found account 1" << endl;
            // Delete selected account.
            delete (*it);
            it = db.erase(db.begin());
        }
        else ++it;
    }
}

我创建了一个小测试文件来测试所有功能:

int main ()
{
    Bank bank;

    cout << "Num accounts in bank: " << bank.getNumAccounts() << endl << endl;

    cout << "Adding accounts to bank..." << endl;
    bank.addChequing(1, 1500.0, "testchq");
    bank.addSavings(1, 2000.0, "testsav");

    cout << "Num accounts in bank: " << bank.getNumAccounts() << endl;
    for (int i = 0; i < bank.getNumAccounts(); ++i)
    {
        if (bank.getAccount(i + 1) == NULL) cout << "Account is NULL" << endl;
        else
        {
            cout << bank.getAccount(i + 1)->getDetails() << endl;
        }
    }
    cout << endl;

    cout << "Deleting account 1..." << endl;
    bank.deleteAccount(1);
    cout << endl;

    cout << "Num accounts in bank: " << bank.getNumAccounts() << endl;
    for (int i = 0; i < bank.getNumAccounts(); ++i)
    {
        if (bank.getAccount(i + 1) == NULL) cout << "Account is NULL" << endl;
        else
        {
            cout << bank.getAccount(i + 1)->getDetails() << endl;
        }
    }
}

这是我运行文件后得到的输出:

Num accounts in bank: 0

Adding accounts to bank...
Num accounts in bank: 2
Account #1 [C] testchq $1500.000000
Account #2 [S] testsav $2000.000000

Deleting account 1...
Searching for account 1
Found account 1

Num accounts in bank: 2
Account #1 [C] [C] $1500.000000
Account #2 [S] testsav $2000.000000

如您所见,它正确地添加了派生的 Account 类,并在没有对象切片的情况下保留了它们的派生类型。在删除功能中,您可以看到删除功能正在找到它应该正确删除的帐户。问题是,虽然它应该删除 Account #1,但它没有,但确实删除了该帐户的名称(如 Account #1 [C] [C] $1500.000000Account #1 [C] testchq $1500.000000 所见)。

我在这里遇到了什么问题?我也不确定我这样做的方法,所以任何改进建议都将不胜感激。

提前致谢!

【问题讨论】:

    标签: c++ pointers inheritance vector erase


    【解决方案1】:

    您正在从副本中删除帐户

    std::vector<T*> getDatabase () const;
    
    std::vector<Account*> db = m_acctDb.getDatabase();
    

    你需要从实际的数据库中删除,所以你希望的用法是:

    std::vector<T*>& getDatabase ();
    const std::vector<T*>& getDatabase () const;
    
    std::vector<Account*>& db = m_acctDb.getDatabase();
    

    【讨论】:

    • 嗨@Barry,感谢您的回答。只是为了澄清我必须进行哪些更改,我必须将 Database.h 中 getDatabase() const 的签名更改为 const std::vector&lt;T*&gt;&amp; getDatabase () const; ?我必须使用const_iterator 来遍历const vector?我将什么传递给it.erase() 函数?我尝试通过it (it = db.erase(it);) 但它给了我一个错误,我不确定我需要通过什么。谢谢!
    • @user3745117 不,你需要它来返回一个非常量引用......因为你要修改它。只是您的旧签名是const 函数。如果你仍然想要一个 const 函数,那必须是一个单独的函数,它返回一个 const ref。
    • 感谢您的澄清,并且效果很好! :) 这比原来的问题稍微多一点,所以你不必回答这个问题,但快速浏览一下,代码在内存泄漏和设计方面是否存在任何问题?再次感谢您的帮助:)
    • @user3745117 可以将修复版发到CodeReview,他们可以给你全面了解。
    猜你喜欢
    • 1970-01-01
    • 2022-01-13
    • 2021-03-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多