开发者

What is wrong with this usage of std::find_if?

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::fin开发者_运维知识库d_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.


You should use _1, not this when binding, and take a value_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.


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 for find_if over a range of ints, the comparison should take in an int rather than vector<int>::iterator. Changing your comparison function to work on shared_ptr<node>s might not fix all your errors, but it should at least account for some of them.


That function's signature should be

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

i.e., it doesn't take an iterator, but the iterator's value_type.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜