2011-05-26 99 views
3

我有一些代码,其中类从基类继承。为什么我的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))'

谁能给我提供一些见解,以我在做什么黄?

+0

mem_fun_ref(bool(Foo :: *)(int,int))''你能告诉我们吗? – 2011-05-26 12:35:47

+0

@Als:'mem_fun_ref'是一个标准的库函数。 – 2011-05-26 12:37:09

+0

@Space:是的,只是检查!我的错。 – 2011-05-26 12:37:39

回答

0

以下是prorotypes为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 T, class A> 
    const_mem_fun1_ref_t<S,T,A> mem_fun_ref (S (T::*f)(A) const); 

mem_fun_ref(bool (Foo<int>::*)(int, int)) dosnt匹配这些&因此错误的。

+0

错过了!谢谢 ! – foo 2011-05-26 12:46:59

3

mem_fun_ref只适用于采取一个或没有参数的函数。要实现你的想法,你必须使用boost::bind(C++ 0x标准库的一部分)。