[英]Issue erasing an element from a list in c++
I have the following code: 我有以下代码:
list<Item> playerItems = player.GetPlayerItems();
list<Item>::iterator iterator;
for (iterator = playerItems.begin(); iterator != playerItems.end(); ++iterator)
{
if (itemToSell == (*iterator).GetName())
{
if ((*iterator).GetCost() < GetShopGold())
{
SetShopItem(*iterator);
SetShopGold(GetShopGold() - (*iterator).GetCost());
player.SetPlayerGold(player.GetPlayerGold() + (*iterator).GetCost());
//The issue is here
iterator = player.GetPlayerItems().erase(iterator);
return true;
}
else
{
cout << "I can't afford that..." << endl;
}
}
}
This is the RemovePlayerItem code: 这是RemovePlayerItem代码:
void Player::RemovePlayerItem(list<Item>::iterator& iterator)
{
iterator = _playerItems.erase(iterator);
}
When I run the code, I get the following error: 运行代码时,出现以下错误:
_DEBUG_ERROR("list erase iterator outside range");
I've googled it and it seems to be related to my iterator, but I'm not sure of the relationship between the error and the iterator. 我已经用谷歌搜索它,它似乎与我的迭代器有关,但是我不确定错误和迭代器之间的关系。
In contrast, if I make the _playerItems variable a global one and then start/end the iterator using this global variable, the code runs fine if I use the following code iterator = player._playerItems.erase(iterator);
相反,如果我将_playerItems变量设置为全局变量,然后使用该全局变量启动/结束迭代器,则如果我使用以下代码iterator = player._playerItems.erase(iterator);
则代码运行良好iterator = player._playerItems.erase(iterator);
EDIT 编辑
This is GetPlayerItems() function 这是GetPlayerItems()函数
list<Item> Player::GetPlayerItems()
{
return _playerItems;
}
Can anyone point me in the right direction with this? 有人可以为此指出正确的方向吗?
iterator
is an iterator into playerItems
. iterator
是playerItems
的迭代器。 It's not clear what player.GetPlayerItems()
returns (since you neglected to show its definition or even a declaration), but whatever it is, playerItems
is a separate, distinct list - a copy of what GetPlayerItems()
returned. 不清楚是什么player.GetPlayerItems()
返回(因为您忽略了显示其定义甚至声明),但是不管它是什么, playerItems
都是单独的,独立的列表playerItems
GetPlayerItems()
返回的副本。
You are then trying to erase from one list using an iterator pointing into the other. 然后,您尝试使用指向另一个列表的迭代器从一个列表中擦除。 This exhibits undefined behavior - someList.erase(iter)
expects iter
to be a valid iterator into someList
. 这表现出未定义的行为someList.erase(iter)
期望iter
是someList
的有效迭代器。
Here you are trying to erase from one list using an iterator pointing into the other. 在这里,您尝试使用指向另一个列表的迭代器从一个列表中擦除。
Why can not you use the iterator = playerItems.erase(iterator); 为什么不能使用iterator = playerItems.erase(iterator); instead of iterator = player.GetPlayerItems().erase(iterator); 而不是iterator = player.GetPlayerItems()。erase(iterator); inside the loop. 在循环内。
Why your trying to accessing the same list with get function call(player.GetPlayerItems()) as you have already a reference available to the same list . 为什么您尝试使用get函数call(player.GetPlayerItems())访问相同列表,因为您已经有可用于相同列表的引用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.