[英]C++ Erasing element from a vector of pointers
我使用指針向量進行繼承,並且我的每個行為都應該如此,但是,我在向量中刪除對象時遇到問題。
我創建了一個包含所有源文件的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);
};
在我的銀行中,我正在添加帳戶對象,其定義如下:
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類,並保留其派生類型而不進行對象切片。 在刪除功能中,您可以看到刪除功能正在查找應該正確刪除的帳戶。 問題是,雖然它應該刪除帳戶#1,但它沒有,但它確實刪除了帳戶的名稱(如Account #1 [C] [C] $1500.000000
vs 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();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.