c++ - C++11 run templated class function with std::thread -
cannot pass function argument std::thread
when class defined template.
compiler: gcc 4.8.2 language: c++11
code:
//---------test.h----------------------- #ifndef test_h #define test_h #include <iostream> #include <thread> using namespace std; template <class t> class test { public: test(); void thr(t n); void testthread(); }; #endif // test_h //---------test.cpp----------------------- #include "test.h" template <class t> test<t>::test() { } template <class t> void test<t>::thr(t n) { cout << n << endl; } template <class t> void test<t>::testthread() { t n = 8; thread t(thr, n); t.join(); } //---------main.cpp----------------------- #include <iostream> using namespace std; #include "test.h" #include "test.cpp" int main() { test<double> tt; tt.testthread(); return 0; }
compiler error:
in file included ../std_threads/main.cpp:5:0: ../std_threads/test.cpp: in instantiation of 'void test<t>::testthread() [with t = double]': ../std_threads/main.cpp:10:19: required here ../std_threads/test.cpp:19:20: error: no matching function call 'std::thread::thread(<unresolved overloaded function type>, double&)' thread t(thr, n); ^ ../std_threads/test.cpp:19:20: note: candidates are: in file included ../std_threads/test.h:4:0, ../std_threads/main.cpp:4: /usr/include/c++/4.8.2/thread:133:7: note: std::thread::thread(_callable&&, _args&& ...) [with _callable = void (test<double>::*)(double); _args = {double&}] thread(_callable&& __f, _args&&... __args) ^ /usr/include/c++/4.8.2/thread:133:7: note: no known conversion argument 1 '<unresolved overloaded function type>' 'void (test<double>::*&&)(double)' /usr/include/c++/4.8.2/thread:128:5: note: std::thread::thread(std::thread&&) thread(thread&& __t) noexcept ^ /usr/include/c++/4.8.2/thread:128:5: note: candidate expects 1 argument, 2 provided /usr/include/c++/4.8.2/thread:122:5: note: std::thread::thread() thread() noexcept = default; ^ /usr/include/c++/4.8.2/thread:122:5: note: candidate expects 0 arguments, 2 provided make: *** [main.o] error 1 20:48:35: process "/usr/bin/make" exited code 2. error while building/deploying project std_threads (kit: desktop) when executing step 'make'
you need specify member function name , pass argument implicit first parameter of non-static member function:
thread t(&test<t>::thr, this, n);
Comments
Post a Comment