Closed dvilelaf closed 4 years ago
You can't pass &Reader::handlerPacket
as that's a member function pointer (it needs an instance of Reader
to work). You can use std::bind
or a lambda for this. e.g. something like
sniffer.sniff_loop(make_sniffer_handler(this, std::bind(&Reader::handlerPacket, this, std::placeholders::_1)));
That was fast, thanks. But how does it work then with &Reader::handlerPDU? (it should also need an instance, shouldn't it?) I dind't know about std::bind nor std::placeholders, sorry. I've just tried your code and I still get a similar error:
../handler.cpp: In constructor 'Reader::Reader()':
../handler.cpp:25:117: error: cannot convert 'std::_Bind_helper<false, bool (Reader::*)(Tins::Packet&), Reader*, const std::_Placeholder<1>&>::type {aka std::_Bind<bool (Reader::*(Reader*, std::_Placeholder<1>))(Tins::Packet&)>}' to 'Tins::HandlerProxy<Reader>::fun_type {aka bool (Reader::*)(Tins::PDU&)}' for argument '2' to 'Tins::HandlerProxy<T> Tins::make_sniffer_handler(T*, typename Tins::HandlerProxy<T>::fun_type) [with T = Reader; typename Tins::HandlerProxy<T>::fun_type = bool (Reader::*)(Tins::PDU&)]'
sniffer.sniff_loop(make_sniffer_handler(this, std::bind(&Reader::handlerPacket, this, std::placeholders::_1)));
Sorry, I didn't see you were using make_sniffer_handler
. Don't use that, just do
sniffer.sniff_loop(std::bind(&Reader::handlerPacket, this, std::placeholders::_1));
That worked great! Thank you so much for your time!
No problem!
I'm working on an small UDP traffic sniffing example using libtins. Everything worked until I started encapsulating everythin into a class. Acording the tutorial (loop sniffing section), the snifer_loop takes a template functor as an argument, and:
Everything works if I use PDU data type, or if I use Packet type with an external function, but once I use Packet type with a method, I get a compiler error:
The tutorial specifies:
And I am using the c++11 switch.