2011-02-08 31 views
6
#include <QtCore/QCoreApplication> 
#include <boost/bind.hpp> 
#include <boost/function.hpp> 

class button 
{ 
public: 

    boost::function<void()> onClick; 
    boost::function<void(int ,double)> onClick2; 
}; 

class player 
{ 
public: 
    void play(int i,double o){} 
    void stop(){} 
}; 

button playButton, stopButton; 
player thePlayer; 

void connect() 
{ 
    //error C2298: 'return' : illegal operation on pointer to member function expression 
    playButton.onClick2 = boost::bind(&player::play, &thePlayer); 
    stopButton.onClick = boost::bind(&player::stop, &thePlayer); 
} 

int main(int argc, char *argv[]) 

{ 

    QCoreApplication a(argc, argv); 
    connect(); 
    return a.exec(); 
} 

Trả lời

13
boost::bind(&player::play, &thePlayer) 

Bạn cần phải sử dụng giữ chỗ cho hai đối số:

boost::bind(&player::play, &thePlayer, _1, _2) 

Các placeholders cho phép bạn nói "Tôi chỉ ràng buộc đối số nhất định; đối số khác sẽ được cung cấp sau."

2

Và nếu bạn muốn tạo mã cầm tay - chỉ định không gian tên của placeholders trực tiếp:

boost::bind(&player::play, &thePlayer, ::_1, ::_2); // Placeholders of boost::bind are placed in global namespace. 
Các vấn đề liên quan