为什么没有“匹配功能”我对 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;}
};
但是,当我这样做时,我收到以下错误消息: 没有匹配的函数来调用'mem_fun_ref(bool (Foo
任何人都可以向我提供一些关于我在做什么的见解吗?
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) {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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
mem_fun_ref
仅适用于带一个参数或不带参数的函数。要实现您的想法,您必须使用boost: :bind
(C++0x 中标准库的一部分)。mem_fun_ref
only works for functions taking one or no argument. To implement what you have in mind you will have to useboost::bind
(part of the standard library in C++0x).以下是
mem_fun_ref
的原型,您的
mem_fun_ref(bool (Foo::*)(int, int))
与这些 & 不匹配。因此出现错误。Following are the prorotypes for
mem_fun_ref
Your
mem_fun_ref(bool (Foo<int>::*)(int, int))
dosnt match any of these & hence the error.