当编译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
含有一些值的函数,我在编译std::find_if函数时得到以下错误:错误C2451:条件表达式类型'重载函数'是非法的。
你应该使用 _1
,不 this
装订时,并采取 value_type
作为函数的参数。
如果这是一个类或结构成员函数,那么就用 bind(func, this, _1)
也许?但如果它是一个类成员函数,它可能应该是静态的,因为它不需要状态。
你提供给我们的比较函数 find_if
不应该接受一个迭代器,而应该接受迭代器所指向的值(甚至更好的是它的一个const引用)。 例如,当为 find_if
在一系列 int
的,比较应考虑到 int
而非 vector<int>::iterator
. 改变您的比较功能,以便在 shared_ptr<node>
的签名可能无法解决所有的错误,但至少可以解决其中的一些问题。
这个函数的签名应该是
bool RequestValues::is_parameter(boost::shared_ptr<node>);
即不取迭代器,但迭代器的 value_type
.