2011-05-26 58 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標準庫的一部分)。