C++问题: boost::bind 接收其他 boost::bind

发布于 2024-09-04 05:12:05 字数 1007 浏览 1 评论 0原文

我想让这段代码正常工作,我该怎么办?

在最后一行给出这个错误。

我做错了什么? 我知道 boost::bind 需要一个类型,但我没有得到。帮助

class A
{

public:

    template <class Handle>
    void bindA(Handle h)
    {
        h(1, 2);
    }
};

class B
{

    public:
        void bindB(int number, int number2)
        {
            std::cout << "1 " << number << "2 " << number2 << std::endl;
        }
};


template < class Han > struct Wrap_
{

    Wrap_(Han h) : h_(h) {}

    template<typename Arg1, typename Arg2> void operator()(Arg1 arg1, Arg2 arg2)
    {
        h_(arg1, arg2);
    }
    Han h_;
};

template< class Handler >

    inline Wrap_<Handler> make(Handler h)
    {
        return Wrap_<Handler> (h);
    }
int main()
{

    A a;
    B b;
    ((boost::bind)(&B::bindB, b, _1, _2))(1, 2);
    ((boost::bind)(&A::bindA, a, make(boost::bind(&B::bindB, b, _1, _2))))();
/*i want compiled success and execute success this code*/

}

I want to make this code work properly, what should I do?

giving this error on the last line.

what am I doing wrong?
i know boost::bind need a type but i'm not getting. help

class A
{

public:

    template <class Handle>
    void bindA(Handle h)
    {
        h(1, 2);
    }
};

class B
{

    public:
        void bindB(int number, int number2)
        {
            std::cout << "1 " << number << "2 " << number2 << std::endl;
        }
};


template < class Han > struct Wrap_
{

    Wrap_(Han h) : h_(h) {}

    template<typename Arg1, typename Arg2> void operator()(Arg1 arg1, Arg2 arg2)
    {
        h_(arg1, arg2);
    }
    Han h_;
};

template< class Handler >

    inline Wrap_<Handler> make(Handler h)
    {
        return Wrap_<Handler> (h);
    }
int main()
{

    A a;
    B b;
    ((boost::bind)(&B::bindB, b, _1, _2))(1, 2);
    ((boost::bind)(&A::bindA, a, make(boost::bind(&B::bindB, b, _1, _2))))();
/*i want compiled success and execute success this code*/

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

回忆躺在深渊里 2024-09-11 05:12:05

您遇到的问题是您正在尝试绑定到模板化函数。在这种情况下,您需要指定要调用绑定的方法的模板类型。

A::bindA 方法就会发生这种情况。请参阅下面的 main 代码片段,该代码片段可以使用提供的类正确编译。

顺便说一句,在示例中我使用 boost::function (要绑定的姐妹库)指定正在使用的函数指针的类型。我认为这使它更具可读性,并且如果您要继续使用绑定,强烈建议您熟悉它。

#include "boost/bind.hpp"
#include "boost/function.hpp"

int main(int c, char** argv)
{
  A a;
  B b;

  typedef boost::function<void(int, int)> BFunc;
  typedef boost::function<void(BFunc)> AFunc;
  BFunc bFunc( boost::bind(&B::bindB, b, _1, _2) );
  AFunc aFunc( boost::bind(&A::bindA<BFunc>, a, make(bFunc)) );

  bFunc(1,2);
}

The problem that you are having is that you are trying to bind to a templated function. In this case you need to specify the template type of the method you are calling to bind.

This is happening for the method A::bindA. See below for a code fragment for main that compiles correctly with the supplied classes.

Incidentally in the example I use boost::function (the sister library to bind) to specify what type of function pointers are being used. I think this makes it far more readable and would highly recommend that you become familiar with it if you are going to continue using bind.

#include "boost/bind.hpp"
#include "boost/function.hpp"

int main(int c, char** argv)
{
  A a;
  B b;

  typedef boost::function<void(int, int)> BFunc;
  typedef boost::function<void(BFunc)> AFunc;
  BFunc bFunc( boost::bind(&B::bindB, b, _1, _2) );
  AFunc aFunc( boost::bind(&A::bindA<BFunc>, a, make(bFunc)) );

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