从列表中删除和删除指针
Delete and remove a pointer from a list
我有这段代码(它是复制错误的代码的 smol 版本),它给出了某种内存错误。 idk 请帮我解决它。它删除了对象,因此只剩下 nullptr。不知道为什么,但它不想从列表中删除指针。
#include <iostream>
#include <list>
// casual include
我在这里创建了一个 class 这是我所有其他 classes
的基础
class Object // a virtual class
{
public:
bool needs_delete = false;
virtual void tick() {}
virtual void render() {}
};
一个播放器class继承自我之前创建的对象class
class Player : public Object
{
public:
float x, y; // <-- just look at da code dont read dis
Player(float x, float y) : // i initialize the "x" & "y" with the x & y the user has set in the constructor
x(x), y(y)
{}
void tick() override // just look at the code
{
x++;
if (x > 10000)
{
needs_delete = true;
}
}
void render() override // just look at the code
{
// nothing...
}
};
只是主要功能。在这一点上,我只是在写文字,因为 Whosebug 不会让我 post 这种持续的沮丧。请帮助:)
int main()
{
std::list<Object*>* myObjs = new std::list<Object*>; // a list that will contain the objects
for (int i = 0; i < 1000; i++) // i create 1k player just for testing
{
myObjs->push_back(new Player(i, 0));
}
while (true)
{
if (myObjs->size() == 0) // if there are no objects i just break out of the loop
break;
for (Object* obj : *myObjs) // update the objects
{
obj->tick();
obj->render();
// some other stuff
}
// DA PART I HAVE NO IDEA HOW TO DO
// pls help cuz i suck
for (Object* obj : *myObjs) // help pls :)
{
// baisicly here i want to delete the object and remove it from the list
if (obj->needs_delete)
{
std::cout << "deleted object\n";
delete obj;
myObjs->remove(obj);
}
}
}
}
怎么样:
myObjs->remove_if([](auto& pObj)
{
if ( pObj->needs_delete )
{
delete pObj;
return true;
}
else
return false;
});
我有这段代码(它是复制错误的代码的 smol 版本),它给出了某种内存错误。 idk 请帮我解决它。它删除了对象,因此只剩下 nullptr。不知道为什么,但它不想从列表中删除指针。
#include <iostream>
#include <list>
// casual include
我在这里创建了一个 class 这是我所有其他 classes
的基础class Object // a virtual class
{
public:
bool needs_delete = false;
virtual void tick() {}
virtual void render() {}
};
一个播放器class继承自我之前创建的对象class
class Player : public Object
{
public:
float x, y; // <-- just look at da code dont read dis
Player(float x, float y) : // i initialize the "x" & "y" with the x & y the user has set in the constructor
x(x), y(y)
{}
void tick() override // just look at the code
{
x++;
if (x > 10000)
{
needs_delete = true;
}
}
void render() override // just look at the code
{
// nothing...
}
};
只是主要功能。在这一点上,我只是在写文字,因为 Whosebug 不会让我 post 这种持续的沮丧。请帮助:)
int main()
{
std::list<Object*>* myObjs = new std::list<Object*>; // a list that will contain the objects
for (int i = 0; i < 1000; i++) // i create 1k player just for testing
{
myObjs->push_back(new Player(i, 0));
}
while (true)
{
if (myObjs->size() == 0) // if there are no objects i just break out of the loop
break;
for (Object* obj : *myObjs) // update the objects
{
obj->tick();
obj->render();
// some other stuff
}
// DA PART I HAVE NO IDEA HOW TO DO
// pls help cuz i suck
for (Object* obj : *myObjs) // help pls :)
{
// baisicly here i want to delete the object and remove it from the list
if (obj->needs_delete)
{
std::cout << "deleted object\n";
delete obj;
myObjs->remove(obj);
}
}
}
}
怎么样:
myObjs->remove_if([](auto& pObj)
{
if ( pObj->needs_delete )
{
delete pObj;
return true;
}
else
return false;
});