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

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

问题描述

这里是我的代码片段。


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;
  }
}

当编译时:

I'm getting the following error when compiling:


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

对不起,我是一个C ++ newb。

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

推荐答案

问题是: bool RoutingProtocolImpl :: hasInfCost(...)是一个非静态成员函数。

The problem is that this: bool RoutingProtocolImpl::hasInfCost(...) is a nonstatic member function.

它需要类的实例调用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

static bool RoutingProtocolImpl :: hasInfCost(RoutingProtocolImpl :: dv_entry * 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天全站免登陆