Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/opengl/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 向量迭代器在冲突检测时不兼容_C++_Opengl - Fatal编程技术网

C++ 向量迭代器在冲突检测时不兼容

C++ 向量迭代器在冲突检测时不兼容,c++,opengl,C++,Opengl,所以我有调试错误(“向量迭代器不兼容”);每当我有黑洞和行星之间的碰撞检测。但是,当我在小行星和行星或黑洞和小行星之间进行碰撞检测时,它的工作没有问题代码基本上是相同的,但是对于这个我有碰撞 //Blackhole& Planet Collide for (vector<Entity*>::iterator it = gameEntities.begin(); it < gameEntities.end(); ++it) // This loop usses an it

所以我有调试错误(“向量迭代器不兼容”);每当我有黑洞和行星之间的碰撞检测。但是,当我在小行星和行星或黑洞和小行星之间进行碰撞检测时,它的工作没有问题代码基本上是相同的,但是对于这个我有碰撞

//Blackhole& Planet Collide
for (vector<Entity*>::iterator it = gameEntities.begin(); it < gameEntities.end(); ++it) // This loop usses an iterator (google c++ iterator) to go through all game entites objects
{
    BlackHole* blackHole1 = dynamic_cast<BlackHole*> (*it); // As the iterator is of generic type Entity we need to convert it to a type we can use, Ball. Dynamic cast performs the conversion only if the entity is actually a Ball otherwise it gives back a null pointer
    if (blackHole1 != nullptr) // we check if the conversion was successful
    {
        for (vector<Entity*>::iterator it1 = gameEntities.begin(); it1 < gameEntities.end(); ++it1) // and we iterate on the remaining elements in the container
        {
            if (it != it1)
            {
                Planet* planet = dynamic_cast<Planet*> (*it1); // we convert the remaining element
                if (planet != nullptr) // check if the conversion happended
                {
                    // collision detection: black hole & planet
                    if (blackHolePlanetCollision(*blackHole1, *planet))
                    {
                        blackHole1->increaseMass(blackHole1->getRadius(), planet->getRadius());
                        delete *it1;
                        it1 = gameEntities.erase(it1);
                        //--it1;
//黑洞与行星碰撞
对于(vector::ItAs= GAMialStudio.NeX),它< GAMialStudio.Enter();+/it)/这个循环使用迭代器(谷歌C++迭代器)来遍历所有游戏实体对象。
{
BlackHole*blackHole1=dynamic_cast(*it);//由于迭代器是泛型类型Entity,我们需要将其转换为我们可以使用的类型Ball。dynamic cast仅在实体实际上是Ball时执行转换,否则它会返回空指针
if(blackHole1!=nullptr)//我们检查转换是否成功
{
对于(vector::iterator it1=gameEntities.begin();it1增加质量(黑洞1->getRadius(),行星->getRadius());
删除*it1;
it1=游戏实体。擦除(it1);
//--it1;
你知道这是怎么回事吗?如果有任何其他冲突,这段代码都能正常工作

it1 = gameEntities.erase(it1);
在这里,您在从向量中删除后正确地重置了
it1
(这可能会使现有迭代器无效)……但您没有对
它执行相同的操作,它现在可能会无效


当它无效时,您稍后尝试将其与新的
it1
进行比较,您的标准库实现发现了该错误,并试图告诉您该比较无效。在发布版本中,我希望这只是默默地做一些奇怪的事情。

据我所知,您希望我添加:it=gameEntities.erase(it);但错误在外部变成了向量擦除迭代器range@maniekv16:不,那将是删除另一个元素。我不知道解决方案是什么,我只是说你需要检查你的算法,因为当你删除
it1
@Lightness Races in Orbital:修复它时,你需要添加它=gameEntities.begin();@maniekv16如果你想再次启动外部循环。不要只是猜测-仔细想想。你对所有类型的碰撞(黑洞/行星,行星/小行星等)都运行相同的N^2算法吗?也许你可以将代码重构为:
for(auto&it_a:gameEntities){for(auto it_b=++it_a;it_b!=gameEntities.end();++it_b){处理碰撞(it_a,it_b)}}