Note that there are some explanatory texts on larger screens.

plurals
  1. POExecuting a member function of a class
    primarykey
    data
    text
    <p>I am trying to experiment the C++11 threads in a way that it accepts a member function of a class as a parameter into the thread constructor as shown in the first code snippet below on line 20 that is marked . The class definition is given in the 2nd code snippet. When this code is compiled, I am getting a bunch of errors shown in the 3rd snippet. Can anyone tell me what I am doing wrong? Thanks.</p> <p><strong>SNIPPET 1: Thread initialization (main_app.cpp)</strong></p> <pre><code>#include &lt;thread&gt; #include "ServiceRegistrar.hpp" #define SERVER_TYPE 100065 #define SERVER_INST_LOWER 1 #define SERVER_INST_UPPER 2 #define TIMEOUT 500000 int main() { ServiceRegistrar sr1(SERVER_TYPE, TIMEOUT, SERVER_INST_LOWER, SERVER_INST_LOWER); /*LINE 20 is the following*/ std::thread t(&amp;ServiceRegistrar::subscribe2TopologyServer, sr1); t.join(); sr1.publishForSRs(); } </code></pre> <p><strong>SNIPPET 2: Class definition</strong></p> <pre><code>class ServiceRegistrar { public: ServiceRegistrar(int serverType, int serverTimeOut, int serverInstanceLower, int serverInstanceUpper) : mServerType(serverType), mServerTimeOut(serverTimeOut), mServerInstanceLower(serverInstanceLower), mServerInstanceUpper(serverInstanceUpper) { } void subscribe2TopologyServer(); void publishForSRs(); void publishForServices(); private: int mServerType; int mServerTimeOut; int mServerInstanceLower; int mServerInstanceUpper; }; </code></pre> <p><strong>SNIPPET 3: Compilation output</strong></p> <pre><code> $ g++ -g -c -Wall -std=c++11 main_app.cpp -pthread In file included from /usr/include/c++/4.7/ratio:38:0, from /usr/include/c++/4.7/chrono:38, from /usr/include/c++/4.7/thread:38, from main_app.cpp:8: /usr/include/c++/4.7/type_traits: In instantiation of ‘struct std::_Result_of_impl&lt;false, false, std::_Mem_fn&lt;void (ServiceRegistrar::*)()&gt;, ServiceRegistrar&gt;’: /usr/include/c++/4.7/type_traits:1857:12: required from ‘class std::result_of&lt;std::_Mem_fn&lt;void (ServiceRegistrar::*)()&gt;(ServiceRegistrar)&gt;’ /usr/include/c++/4.7/functional:1563:61: required from ‘struct std::_Bind_simple&lt;std::_Mem_fn&lt;void (ServiceRegistrar::*)()&gt;(ServiceRegistrar)&gt;’ /usr/include/c++/4.7/thread:133:9: required from ‘std::thread::thread(_Callable&amp;&amp;, _Args&amp;&amp; ...) [with _Callable = void (ServiceRegistrar::*)(); _Args = {ServiceRegistrar&amp;}]’ main_app.cpp:20:64: required from here /usr/include/c++/4.7/type_traits:1834:9: error: no match for call to ‘ (std::_Mem_fn&lt;void (ServiceRegistrar::*)()&gt;) (ServiceRegistrar)’ </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload