std::find_if 的这种用法有什么问题?
编译 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
。
I get the following error when compiling the std::find_if function:
error C2451: conditional expression of type 'overloaded-function' is illegal
The code looks like this:
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);
}
It seems to have something to do with the predicate function passed to the std::find_if
, however I cannot figure out what is wrong, can anybody help?
node
is a struct
containing some values.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
绑定时您应该使用
_1
,而不是this
,并采用value_type
作为函数的参数。如果这是一个类或结构成员函数,那么可能是bind(func, this, _1) ?但如果它是一个类成员函数,它可能应该是静态的,因为它不需要声明。
You should use
_1
, notthis
when binding, and take avalue_type
as an argument of your function.If this is a class or struct member function, then
bind(func, this, _1)
maybe? But if it is a class member function it should probably be static because it needn't state.您提供给 find_if 的比较函数不应采用迭代器,而应采用迭代器指向的值(或者更好的是对其进行 const 引用)。例如,当在一系列
int
上为find_if
编写谓词时,比较应采用int
而不是vector< ;int>::迭代器
。更改比较函数以在shared_ptr
上工作可能无法修复所有错误,但它至少应该解决其中的一些错误。The comparison function you provide to
find_if
should not take in an iterator, but rather the value that the iterator is pointing at (or even better, a const reference to it). For example, when writing a predicate forfind_if
over a range ofint
s, the comparison should take in anint
rather thanvector<int>::iterator
. Changing your comparison function to work onshared_ptr<node>
s might not fix all your errors, but it should at least account for some of them.该函数的签名应该是
,即它不接受迭代器,而是接受迭代器的
value_type
。That function's signature should be
i.e., it doesn't take an iterator, but the iterator's
value_type
.