[英]c++ remove custom object from vector : std::remove_if': no matching overloaded function found
In my project there is a vector 在我的项目中有一个向量
std::vector<std::shared_ptr<MovingEntity>>gameObjects;
Which I want to delete elements from if they meet the criteria. 如果元素符合条件,我要从中删除。
Method to delete elements: 删除元素的方法:
void GameWorld::catchBees()
{
auto q = std::remove_if(bees.begin(), bees.end(), beeToClose);
bees.erase(q);
}
Method beeToClose: 方法beeToClose:
bool GameWorld::beeToClose( const MovingEntity & bee)
{
std::shared_ptr<Beekeeper> keeper = std::static_pointer_cast<Beekeeper>(m_beekeeper);
if (bee.getConstPosition().distanceTo(m_beekeeper->getPosition()) > keeper->getCatchDistance())
{
return true;
}
return false;
}
When I try to compile the code I get some errors which I tried to understand: 当我尝试编译代码时,出现一些我试图理解的错误:
'GameWorld::beeToClose': non-standard syntax; 'GameWorld :: beeToClose':非标准语法; use '&' to create a pointer 使用“&”创建指针
Not sure why this message is given 不确定为什么给出此消息
'std::remove_if': no matching overloaded function found 'std :: remove_if':找不到匹配的重载函数
I did not declare beeToClose right? 我没有声明beeToClose对吗?
'q': cannot be used before it is initialized SDLFramework 'q':在初始化SDLFramework之前不能使用
q is not initialized because: q未初始化是因为:
std::remove_if(bees.begin(), bees.end(), beeToClose);
does not run correct? 运行不正确?
How can I remove a std::shared_ptr correctly from a vector correctly when meeting some criteria? 满足某些条件时,如何从向量中正确删除std :: shared_ptr?
The syntax for forming a pointer to member function is &ClassName::FunctionName
. 形成指向成员函数的指针的语法为&ClassName::FunctionName
。 So you need &GameWorld::beeToClose
for a pointer to the beeToClose
member function. 因此,您需要&GameWorld::beeToClose
作为指向beeToClose
成员函数的指针。 In your case, you should use a lambda from which you call that function 在您的情况下,您应该使用一个lambda来调用该函数
auto q = std::remove_if(bees.begin(), bees.end(),
[&](shared_ptr<MovingEntity> const& bee){ return beeToClose(bee); });
Also, you're using the wrong vector::erase
overload, you want the one that erases a range of elements, not the one that erases a single element. 另外,您使用的是错误的vector::erase
重载,您想要的是一种擦除一系列元素的方法,而不是一种擦除单个元素的方法。
bees.erase(q, bees.end());
The vector
contains std::shared_ptr<MovingEntity>
elements, so beeToClose()
needs to accept a const std::shared_ptr<MovingEntity> &
parameter as input, not a const MovingEntity &
parameter. 该vector
包含std::shared_ptr<MovingEntity>
元素,因此beeToClose()
需要接受const std::shared_ptr<MovingEntity> &
参数作为输入,而不是const MovingEntity &
参数。 Also, beeToClose()
appears to be a non-static class method that accesses a non-static class member ( m_beekeeper
), so you can't just pass beeToClose()
directly to std::remove_if()
as it does not have access to the calling object's this
pointer, but you can wrap it in a lambda to capture the this
pointer. 另外, beeToClose()
似乎是访问非静态类成员( m_beekeeper
)的非静态类方法,因此您不能仅将beeToClose()
直接传递给std::remove_if()
因为它没有访问权限指向调用对象的this
指针,但是您可以将其包装在lambda中以捕获this
指针。
Try this: 尝试这个:
void GameWorld::catchBees()
{
auto q = std::remove_if(bees.begin(), bees.end(),
[this](const const std::shared_ptr<MovingEntity> &bee) {
return this->beeToClose(bee);
}
);
bees.erase(q, bees.end());
}
bool GameWorld::beeToClose(const std::shared_ptr<MovingEntity> &bee)
{
std::shared_ptr<Beekeeper> keeper = std::static_pointer_cast<Beekeeper>(m_beekeeper);
return (bee->getConstPosition().distanceTo(m_beekeeper->getPosition()) > keeper->getCatchDistance());
}
You might also consider moving the distance calculation into Beekeeper
instead: 您也可以考虑将距离计算移至Beekeeper
:
bool GameWorld::beeToClose(const std::shared_ptr<MovingEntity> &bee)
{
std::shared_ptr<Beekeeper> keeper = std::static_pointer_cast<Beekeeper>(m_beekeeper);
return !keeper->isInCatchDistance(bee);
}
bool Beekeeper::isInCatchDistance(const std::shared_ptr<MovingEntity> &bee)
{
return (bee->getConstPosition().distanceTo(getPosition()) <= getCatchDistance());
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.