让我们考虑以下代码部分:
template <class T, class Comp>
Class MyClass {
public:
struct my_functor {
public:
my_functor(const T w) : wanted(w) {}
bool operator()(const T t) {
return customEquals(t,wanted);
}
private:
T wanted;
};
private:
std::vector<T> container;
bool customEquals(T a, T b) { ... }
};
当我进一步尝试致电时
std::find_if(container.begin(),container.end(),v,my_functor(v));
如果v是类型T const&
的输入,我得到以下编译错误(g ++,C ++ 14):
error: no matching function for call to 'find_if(std::vector<int>::iterator,std::vector<int>::iterator,const int&,MyClass<int,bool(*)(int,int)>::my_functor)
T
在此特定示例中为int
。
这里似乎有什么问题?
PS:我已经包含了所有必需的标题(算法,矢量,功能......),所以问题不存在。
答案 0 :(得分:2)
这里似乎有什么问题?
不确定为什么您希望此代码能够正常运行。
首先,您需要检查std::find_if
重载。没有重载接受查找值和自定义谓词,只接受谓词。
然后,如果您使用my_functor
,代码应如下所示:
std::find_if(std::cbegin(container), std::cend(container),
MyClass<T, Comp>::my_functor(v));
T
和Comp
是某些类型。