error: no matching function for call to ‘bind(<unresolved overloaded function type>,
-
#include <thread> #include <functional> #include <iostream> class A { public: void doo() { std::cout << "doo" << std::endl; } void doo(const int x) { std::cout << "x: " << x << std::endl; } }; int main() { A obj; std::thread(std::bind(&A::doo, &obj)); return 0; }
main.cpp:17:37: error: no matching function for call to ‘bind(<unresolved overloaded function type>, A*)’ main.cpp:17:37: note: candidates are: /usr/lib/gcc/i686-pc-linux-gnu/4.6.1/../../../../include/c++/4.6.1/functional:1444:5: note: template<class _Functor, class ... _ArgTypes> typename std::_Bind_helper::type std::bind(_Functor&&, _ArgTypes&& ...) /usr/lib/gcc/i686-pc-linux-gnu/4.6.1/../../../../include/c++/4.6.1/functional:1471:5: note: template<class _Result, class _Functor, class ... _ArgTypes> typename std::_Bindres_helper::type std::bind(_Functor&&, _ArgTypes&& ...)
Irgendwelche Ideen, was ich falsch mache?
-
std::thread(std::bind(static_cast<void (A::*)()>(&A::doo), &obj));
Falls jemand etwas einfacheres kennt, wäre ich ebenfalls daran interessiert.
-
SeppJ schrieb:
std::thread(std::bind(static_cast<void (A::*)()>(&A::doo), &obj));
Falls jemand etwas einfacheres kennt, wäre ich ebenfalls daran interessiert.
Für die Hilfe!
Ich finde die Lösung echt frickelig. (Nichts gegen dich, es ist so wie ich im Internet gelesen habe, die einzige Lösung!)
Ich habe gehofft, dass es was ohne Konvertierungen gibt.Wieso funktioniert es ohne Fehlermeldung, wenn ich boost::bind & boost::thread verwende?
#include <boost/thread.hpp> #include <boost/bind.hpp> #include <iostream> class A { public: void doo() { std::cout << "doo" << std::endl; } void doo(const int x) { std::cout << "x: " << x << std::endl; } }; int main() { A obj; boost::thread(boost::bind(&A::doo, &obj)); return 0; } // link with -lboost_thread
Compilerbug? Standard hat es so definiert?
Ich hoffe ersteres, wenn letzteres dannfür den Standard.
-
Seit Lambdas mit C++11 benutze ich std::bind gar nicht/extrem selten.
boost::thread([&obj] () { obj.doo(); });
-
Ja, gerade bei überladenen Funktionen, sind Lambdas sehr praktisch.
-
Vor allem spart man sich die kryptischen Fehlermeldungen, die bei std::bind auftreten können. Nebenbei wirds auch (meiner Meinung nach) lesbarer.