I have a class that fires events, and I want to handle those event in another class. The following code shows what I am trying to achieve:
#include <functional>
using namespace std;
class StatusReport {
public:
int value;
};
class Sender {
public:
function<void(StatusReport *report)> ReportEvent;
void test() {
StatusReport *r = new StatusReport();
ReportEvent(r);
}
};
class Receiver {
public:
Receiver() {
Sender s = Sender();
s.ReportEvent = std::bind(&Receiver::StatusReportEvent, this);
}
void StatusReportEvent(StatusReport *report) {
}
};
int main() {
Receiver r();
return 0;
}
This generates an error when I try to bind the event to a method inside my receiver object. The error is
no match for call to ‘(std::_Bind<std::_Mem_fn<void (Receiver::*)(StatusReport*)>(Receiver*)>) (StatusReport*)’
What am I doing wrong here?
Since your function receives argument of type StatusReport
bind should be called like
s.ReportEvent = std::bind
(
&Receiver::StatusReportEvent, this, std::placeholders::_1
);
std::placeholders::_1
is a placeholder for argument.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.