【发布时间】:2018-07-19 19:07:33
【问题描述】:
我的目标是拥有一个庞大的全球弹道对象向量,以跟踪当前处于活动状态的每一发子弹/子弹/导弹。但是,我还需要每个图块知道哪个弹道对象直接位于其上方(用于渲染目的),所以我试图让每个图块都有一个小的整数向量,每个都是更大 BallistsVector 的索引来描述哪些在任何给定的帧上都高于它
我的问题是我试图从整数向量中删除一个整数,但是我不断得到:
no instance of overloaded function "std::vector<_Ty, _Alloc>::erase [with _Ty=int, _Alloc=std::allocator<int>]" matches the argument list
std::_Vector_iterator<std::_Vector_val<std::_Simple_types<int>>> std::vector<int,std::allocator<_Ty>>::erase(std::_Vector_const_iterator<std::_Vector_val<std::_Simple_types<int>>>,std::_Vector_const_iterator<std::_Vector_val<std::_Simple_types<int>>>)': cannot convert argument 1 from 'int' to 'std::_Vector_const_iterator<std::_Vector_val<std::_Simple_types<int>>>
我有:
extern std::vector<BallisticObject> BallisticVector;
我有 tile.h:
class Tile
{
public:
Tile(float height, char type);
~Tile();
unsigned char myType;
float myHeight;
//future optimization: change below to forward_list
std::vector<int> Indices_of_Ballistic_Objects_Above_Me;
};
我有一个来自 BallisticsObject.cpp 的 sn-p:
void BallisticObject::Explode()
{
int TileOverX = location.x / 64;
int TileOverY = location.y / 64;
if (TileOverX >= 0 && TileOverX < MAP_WIDTH && TileOverY >= 0 && TileOverY < MAP_HEIGHT) //if it's within bounds
if (location.z <= Map[TileOverX][TileOverY]->myHeight + 1.0f) //if it's close to the ground
{
this->active = 0;
Map[TileOverX][TileOverY]->myHeight -= 1.0f; //chip away at the ground
//now look through the vector of integers (which are indices into the global ballistics vector) and find myself (one that matches my own index)
for (int i = 0; i < Map[TileOverX][TileOverY]->Indices_of_Ballistic_Objects_Above_Me.size(); i++)
{
if (Map[TileOverX][TileOverY]->Indices_of_Ballistic_Objects_Above_Me[i] == myIndex) //found myself
{
//remove myself from the tile's list of ballistics object that are above it
Map[TileOverX][TileOverY]->Indices_of_Ballistic_Objects_Above_Me.erase(i);
break;
}
std::cout << "Error: ballistics object couldn't find itself in the tile's vector of indices!\n";
}
}
}
我有一种挥之不去的感觉,我要么做错了一件小事,要么做错了一件大事......
【问题讨论】:
-
你必须传递一个迭代器 - 快速但混乱的 hack 是
Map[TileOverX][TileOverY]->Indices_of_Ballistic_Objects_Above_Me.erase(Map[TileOverX][TileOverY]->Indices_of_Ballistic_Objects_Above_Me.begin()+i); -
..就这样,它立即编译并运行!非常感谢!
-
以后请尽量减少你的描述和代码,专注于你的问题。我们这里说的是项目符号真的没关系,而且相关的代码也不过几行而已。
标签: c++ vector int erase const-iterator