指针向量中的C ++擦除元素

用户名

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

我创建了一个包含所有源文件的Github要点,以及一个易于编译和运行的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);
    };

在我的银行中,我要添加“帐户”对象,它们的定义如下:

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 ()
        {
        }
};

我正在向银行添加帐户对象,主要是从基本帐户类派生类的支票账户/储蓄帐户:

/*
 * 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类,并保留了它们的派生类型,而没有对象切片。在删除功能中,您可以看到删除功能正在查找应该正确删除的帐户。问题在于,尽管它应该删除了帐户1,但并没有删除,但是确实删除了帐户名称(如Account #1 [C] [C] $1500.000000vs所示Account #1 [C] testchq $1500.000000)。

我在这里遇到什么问题?我也不确定自己的执行方法,因此任何改进建议都将不胜感激。

提前致谢!

巴里

您正在从副本中删除帐户

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();

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

指针向量中的C ++擦除元素

来自分类Dev

C++ 擦除向量元素

来自分类Dev

擦除结构向量的元素

来自分类Dev

如何通过指针擦除向量元素?

来自分类Dev

使用for循环擦除向量中的元素

来自分类Dev

从向量中擦除元素– rbegin()vs begin()

来自分类Dev

如何修复从向量中擦除元素的错误?

来自分类Dev

C++ 按值擦除向量元素

来自分类Dev

从2D向量中擦除元素C ++

来自分类Dev

在C ++中遍历向量时如何擦除或更改元素?

来自分类Dev

vector :: erase不会擦除期望的元素,而是擦除了向量中的最后一个元素

来自分类Dev

从struct类型列表中擦除元素

来自分类Dev

使用指针访问struct c ++向量中的元素

来自分类Dev

在C ++中从向量擦除对象

来自分类Dev

在向量中添加或删除之后保留指向向量元素的指针(在c ++中)

来自分类Dev

给定一个向量,擦除低于 itemsnum 的元素

来自分类Dev

C ++列表会擦除列表中的end()-1个元素

来自分类Dev

C ++列表会擦除列表中的end()-1个元素

来自分类Dev

从向量擦除后重复的指针

来自分类Dev

使用擦除删除类对象数组中的元素

来自分类Dev

提交文本输入会擦除 JavaScript 中的页面元素

来自分类Dev

c++ maperase(),迭代器打印擦除元素

来自分类Dev

C ++,擦除向量

来自分类Dev

我可以直接从C ++中的向量元素的指针获取索引吗?

来自分类Dev

指针向量和擦除特定值

来自分类Dev

指向向量元素的C ++指针,元素已删除

来自分类Dev

指向向量元素的C ++指针,元素已删除

来自分类Dev

向量迭代器在条件下擦除两个元素

来自分类Dev

提高ptr向量的正确方法来擦除一个元素

Related 相关文章

热门标签

归档