Trying to get the function object from std::bind()
using following code:
driver_manager driverManager();
std::function<void(mqtt::const_message_ptr,
mqtt::async_client*,callback*,sql::Driver*)> fn = std::bind(&driver_manager::test_callback, &driverManager, std::placeholders::_1,
std::placeholders::_2, std::placeholders::_3, std::placeholders::_4);
Results in error:
error: conversion from ‘std::_Bind_helper<false, void (driver_manager::*)(std::shared_ptr<const mqtt::message>, mqtt::async_client*, callback*, sql::Driver*), driver_manager (*)(), const std::_Placeholder<1>&, const std::_Placeholder<2>&, const std::_Placeholder<3>&, const std::_Placeholder<4>&>::type’ to non-scalar type ‘std::function<void(std::shared_ptr<const mqtt::message>, mqtt::async_client*, callback*, sql::Driver*)>’ requested
I have been trying to find out what's wrong for hours, what is wrong with this code?
EDIT
driver_manager.hpp
class driver_manager{
public:
driver_manager();
void test_callback(mqtt::const_message_ptr, mqtt::async_client*,callback*,sql::Driver*);
};
driver_manager.cpp
driver_manager::driver_manager(){
}
void driver_manager::test_callback(mqtt::const_message_ptr msg, mqtt::async_client *client, callback *cb, sql::Driver *driver){
std::cout << "Callback triggered" << std::endl;
}
As @Patrick Roberts pointed out in the comments, I was trying to pass a function to std::bind, when an instance of driver_manager
was needed. The solution was to change
driver_manager driverManager();
to
driver_manager driverManager;
.
Referred to [StackOverflow answer] https://stackoverflow.com/a/9490384/11693641 for more details.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With