1

考虑这个例子:

#include <boost/signals2/signal.hpp>
#include <boost/bind.hpp>

typedef boost::signals2::signal< void ( double ) > DoubleSignalType;
typedef boost::signals2::signal< void ( void ) >   VoidSignalType;

class B {
public:
    void connect( DoubleSignalType::slot_type dbl_slot ) {
        dbl_sig.connect( dbl_slot );
    }

    void connect( VoidSignalType::slot_type void_slot ) {
        void_sig.connect( void_slot );
    }
private:
    DoubleSignalType dbl_sig;
    VoidSignalType void_sig;
};

class A {
public:
    void foo( double a ) {};
    void bar( void ) {};
    void other(){
        B b;
        b.connect( boost::bind( &A::foo, this, _1 ) );
    }
};

int main( int argc, char* argv[] ) { return 0; }

当我编译这个时,我得到:

g++ x.cpp
x.cpp: In member function ‘void A::other()’:
x.cpp:27:53: error: call of overloaded ‘connect(boost::_bi::bind_t<void, boost::_mfi::mf1<void, A, double>, boost::_bi::list2<boost::_bi::value<A*>, boost::arg<1> > >)’ is ambiguous
x.cpp:27:53: note: candidates are:
x.cpp:9:10: note: void B::connect(boost::signals2::signal1<void, double, boost::signals2::optional_last_value<void>, int, std::less<int>, boost::function<void(double)>, boost::function<void(const boost::signals2::connection&, double)>, boost::signals2::mutex>::slot_type)
x.cpp:13:10: note: void B::connect(boost::signals2::signal0<void, boost::signals2::optional_last_value<void>, int, std::less<int>, boost::function<void()>, boost::function<void(const boost::signals2::connection&)>, boost::signals2::mutex>::slot_type)

有办法吗?

4

2 回答 2

2

一种简单的解决方案是显式创建所需的信号类型。IE:

    b.connect( DoubleSignalType::slot_type( boost::bind( &A::foo, this, _1 ) ) );
于 2013-04-11T09:26:07.527 回答
2

你必须做一些更复杂的事情,因为boost::bind'soperator()是模板,并且有任意数量的参数(能够调用没有参数的函数,因为它有一些)。

如果您提供非重载方法,一切都会好起来的

于 2013-04-11T09:27:51.333 回答