我的问题涉及以下功能:
/*Adds the transaction to the head of the linked list attached to the account.*/
void Bank::Worker::UpdateTransactionHistory(int account_number, string transaction, Bank *our_bank_ptr) {
transaction_node new_trans;
new_trans.transaction = transaction;
if (our_bank_ptr->accounts[account_number].head == nullptr) { //If this is the first transaction
our_bank_ptr->accounts[account_number].head = &new_trans;
} else { //If this isn't the first transaction, disconnect the account from its current transaction list, connect the new transaction to the account and then connect the old list to the new transaction.
transaction_node temp;
temp = *(our_bank_ptr->accounts[account_number].head);
our_bank_ptr->accounts[account_number].head = &new_trans;
new_trans.next = &temp;
}
if (our_bank_ptr->accounts[account_number].head) //here the correct string is printed
cout << our_bank_ptr->accounts[account_number].head->transaction;
}
它旨在更新 new_trans 的交易字段,然后将其链接到给定帐户的交易列表的其余部分。就在我从更新事务函数返回之前,我测试以确保正确添加了字符串。函数的最后一行是cout << our_bank_ptr->accounts[account_number].head->transaction;
正确输出交易字符串。
但是,当我从函数返回并立即调用完全相同的代码行时,编译器会告诉我,函数更新的事务字段仍未初始化。尽管它是作为指针传入的。
有没有搞错!?我认为如果我通过指针将信息传递给函数,那么我在函数过程中对该信息所做的任何事情都是永久性的?我在这里想念什么?
感谢您的帮助,
亚当