这个std :: find_if有什么问题

时间:2011-02-03 08:56:05

标签: predicate c++

编译std :: find_if函数时出现以下错误:

error C2451: conditional expression of type 'overloaded-function' is illegal

代码如下所示:

typedef std::vector<boost::shared_ptr<node> >::iterator nodes_iterator;

nodes_iterator node_iter = std::find_if(_request->begin(), _request->end(), boost::bind(&RequestValues::is_parameter, this));

bool RequestValues::is_parameter(nodes_iterator iter)
{
   return ((*iter)->name.compare("parameter") == 0);
}

它似乎与传递给std::find_if的谓词函数有关,但我无法弄清楚出了什么问题,有人可以帮忙吗?

node是包含一些值的struct

3 个答案:

答案 0 :(得分:3)

绑定时应使用_1,而不是this,并使用value_type作为函数的参数。

如果这是一个类或结构成员函数,那么bind(func, this, _1)可能吗?但如果它是类成员函数,它应该是静态的,因为它不需要说明。

答案 1 :(得分:1)

您提供给find_if的比较函数不应该接受迭代器,而应该接受迭代器指向的值(或者更好的是对它的const引用)。例如,在find_if范围内为int编写谓词时,比较应采用int而不是vector<int>::iterator。更改比较功能以使用shared_ptr<node>可能无法修复所有错误,但至少应该考虑其中一些错误。

答案 2 :(得分:1)

该功能的签名应为

bool RequestValues::is_parameter(boost::shared_ptr<node>);

即,它不需要迭代器,而是迭代器的value_type