将函数传递给remove_if时,C ++编译错误 [英] C++ compilation error when passing a function into remove_if

查看:120
本文介绍了将函数传递给remove_if时,C ++编译错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,这里是我的代码片段。

So here's a snippet of my code.


void RoutingProtocolImpl::removeAllInfinity()
{
  dv.erase(std::remove_if(dv.begin(), dv.end(), hasInfCost), dv.end()); 
}

bool RoutingProtocolImpl::hasInfCost(RoutingProtocolImpl::dv_entry *entry)
{
  if (entry->link_cost == INFINITY_COST)
  {
    free(entry);
    return true;
  }
  else
  {
    return false;
  }
}

编译时:


RoutingProtocolImpl.cc:368: error: argument of type bool (RoutingProtocolImpl::)(RoutingProtocolImpl::dv_entry*)' does not matchbool (RoutingProtocolImpl::*)(RoutingProtocolImpl::dv_entry*)'

对不起,我是怎样的一个C ++福利局的。

Sorry, I'm kind of a C++ newb.

推荐答案

问题是这样的:布尔RoutingProtocolImpl :: hasInfCost(...)非静态成员函数

它需要类的实例调用on,ala: obj-> hasInfCost(...)。但是, remove_if 不关心并尝试将其称为 hasInfCost(...)。这些不兼容。

It requires an instance of the class to invoke on, ala: obj->hasInfCost(...). However, remove_if cares not and tries to call it as hasInfCost(...). These are incompatible.

您可以做的是使它 static

静态布尔RoutingProtocolImpl :: hasInfCost(RoutingProtocolImpl :: dv_entry *进入)此不再需要类调用的一个实例。 (它没有成员变量, this 指针等)。它可以被视为正常功能。

static bool RoutingProtocolImpl::hasInfCost(RoutingProtocolImpl::dv_entry *entry) This no longer requires an instance of the class to invoke. (It has no member variables, no this pointer, etc.). It can be treated as a "normal" function.

这篇关于将函数传递给remove_if时,C ++编译错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆