我正在开展一个项目,其中我有一个帐户类,用于存储不同基金的余额。每个账户对象有10个基金,从0开始,可以进行存款,取款和转账等交易。我需要将这些帐户存储在二进制搜索树中,因为它们已添加,我的检索功能出现问题。代码如下:
if (transType == "D")
{
iss >> acctNum >> amt;
fund = parseCommand(acctNum);
acctNum = acctNum.substr(0, acctNum.length() - 1);
Account * d = new Account("name", stoi(acctNum));
if (bST->Retrieve(stoi(acctNum), d))
{
d->deposit(fund, amt);
cout << d->getFundBalance(fund) << endl; //for checking, will remove
}
}
else if (transType == "W")
{
iss >> acctNum >> amt;
fund = parseCommand(acctNum);
acctNum = acctNum.substr(0, acctNum.length() - 1);
Account * wD = new Account("name", stoi(acctNum));
if (bST->Retrieve(stoi(acctNum), wD))
{
wD->withdraw(fund, amt);
cout << wD->getFundBalance(fund) << endl; //for checking, will remove
}
}
我遇到的问题是当我存入帐户然后检索并尝试撤销时,它不会给我相同的帐户。相反,它只是试图退出所有0余额的帐户。我的意图是让acctPtr指向正确的账户来进行转账/取款/存款。以下是我如何从用于完成事务的其他类调用检索:
prjList.clear()
上述if语句只是在给定时间检查交易类型。
答案 0 :(得分:1)
在“搜索”功能中,使用参数acctPtr作为输出(为其指定了新值)。
但是你的指针不是输出参数。
您应该使用帐户**或帐户*&amp;。
你将不得不使用这样的Retrieve方法:
Account* d = NULL;
if(bST->Retrieve(stoi(acctNum),&d /* or just d if Account*& */))
{ ... }
如果您使用Account **版本,请不要忘记使用
指定指针*acctPtr = temp->pAcct;