I have a boost::Asio based serial port reader that uses a callback, as follows:
void received(const char *data, unsigned int len)
{
string s(data, len);
cout << s << endl;
}
int main()
{
try {
CallbackAsyncSerial serial("COM4", 115200);
serial.setCallback(received);
for(;;)
{
if(serial.errorStatus() || serial.isOpen()==false)
{
cerr<<"Error: serial port unexpectedly closed"<<endl;
break;
}
char c;
cin.get(c); //blocking wait for standard input
serial.write(&c,1);
}
quit:
serial.close();
} catch (std::exception& e) {
cerr<<"Exception: "<<e.what()<<endl;
}
}
This, as above, compiles and runs. Now I need to merge it into an existing class, so i have:
void MyClass::received(const char *data, unsigned int len)
{
string s(data, len);
cout << s << endl;
}
int MyClass::main()
{
try {
CallbackAsyncSerial serial("COM4", 115200);
serial.setCallback(received); //COMPILE ERROR HERE
for(;;)
{
if(serial.errorStatus() || serial.isOpen()==false)
{
cerr<<"Error: serial port unexpectedly closed"<<endl;
break;
}
char c;
cin.get(c); //blocking wait for standard input
serial.write(&c,1);
}
quit:
serial.close();
} catch (std::exception& e) {
cerr<<"Exception: "<<e.what()<<endl;
}
}
However, adding MyClass::received
stops the compile, giving me:
Error (active) E0415 no suitable constructor exists to convert from "void (const char *data, unsigned int len)" to "std::function<void (const char *, size_t)>"
I have tried adjusting it to:
std::function< void() >received(const char *data, unsigned int len);
But i see the same thing. What do i need to do here?