I have some code where classes inherit from a base class.
That base class has a function which, when run, ought to call functions to be implemented by the children. That is, the general algorithm is the same for all children, but the implementation of the steps should vary.
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) 开发者_JAVA技巧{return x == y;}
};
However, when I do so, I get the following error message :
no matching function for call to 'mem_fun_ref(bool (Foo<int>::*)(int, int))'
Could anyone provide me with some insight as to what I am doing wong?
mem_fun_ref
only works for functions taking one or no argument. To implement what you have in mind you will have to use boost::bind
(part of the standard library in C++0x).
Following are the prorotypes for 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);
Your mem_fun_ref(bool (Foo<int>::*)(int, int))
dosnt match any of these & hence the error.
精彩评论