如何解决为什么我对mem_fun_ref的调用没有“匹配功能”?
| 我有一些代码,其中类从基类继承。 该基类具有一个函数,该函数在运行时应调用要由子代实现的函数。也就是说,所有子代的通用算法都相同,但是步骤的实现应有所不同。template<class T>
class Foo
{
public:
Foo(T y):y(y) { for(int i; i < 10; ++i) x.push_back(i); };
protected:
virtual bool IsOk(T,int)=0;
void Run()
{
vector<int>::iterator it,bound;
for(int i; i < 10; ++i)
{
cout << \"step \" << i << endl;
bound = partition(x.begin(),x.end(),bind2nd(mem_fun_ref(&Foo<T>::IsOk),i));
for (it=x.begin(); it!=bound; ++it)
cout << \" \" << *it;
};
};
private:
vector<int>x;
T y;
};
class Bar : public Foo<int>
{
public:
Bar():Foo<int>(50){this->Run();};
bool IsOk(int x,int y) {return x == y;}
};
但是,这样做时,我收到以下错误消息:
no matching function for call to \'mem_fun_ref(bool (Foo<int>::*)(int,int))\'
谁能为我提供一些有关我在做什么的见解?
解决方法
以下是
mem_fun_ref
的原型
template <class S,class T>
mem_fun_ref_t<S,T> mem_fun_ref (S (T::*f)());
template <class S,class T,class A>
mem_fun1_ref_t<S,T,A> mem_fun_ref (S (T::*f)(A));
template <class S,class T>
const_mem_fun_ref_t<S,T> mem_fun_ref (S (T::*f)() const);
template <class S,class A>
const_mem_fun1_ref_t<S,A> mem_fun_ref (S (T::*f)(A) const);
您的mem_fun_ref(bool (Foo<int>::*)(int,int))
档案与其中任何一项都匹配,因此会出现错误。
,mem_fun_ref
仅适用于带有一个或不带参数的函数。要实现您的想法,您将必须使用boost::bind
(C ++ 0x标准库的一部分)。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。