为什么没有“匹配功能”我对 mem_fun_ref 的调用?

发布于 2024-11-10 10:51:04 字数 1072 浏览 6 评论 0原文

我有一些代码,其中类继承自基类。

该基类有一个函数,该函数在运行时应该调用由子类实现的函数。也就是说,一般算法对于所有孩子来说都是相同的,但步骤的实现应该有所不同。

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::*)(int, int))'

任何人都可以向我提供一些关于我在做什么的见解吗?

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

盛装女皇 2024-11-17 10:51:04

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 use boost::bind (part of the standard library in C++0x).

梦明 2024-11-17 10:51:04

以下是 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)) 与这些 & 不匹配。因此出现错误。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文