title: C++ start thread with method of internal class
body: <p>I have a class B that contains an internal class A. The class A has a method that needs to access some members of B, so it has as one argumet a reference to the external class and one other argument.
  Now i would like to start this method in another thread from B.</p>
  <p>I know this might be a duplicate of this question: <a href="https://stackoverflow.com/questions/10673585/start-thread-with-member-function">Start thread with member function</a>
  But my following minimal example isn't compiling. It sais:</p>
  <p><code>Error    C2672   &quot;std::invoke&quot;: No matching overloaded function found.</code></p>
  <p><code>Error    C2893   Failed to specialize function template &quot;unknown-type std::invoke(_Callable &amp;&amp;,_Types &amp;&amp;...) noexcept(&lt;expr&gt;)&quot;.</code></p>
  <p>This might be related to the pass by reference, but using <code>std::ref</code> does not help me at all.</p>
  <p><strong>B.h</strong></p>
  <pre><code>#include &lt;thread&gt;
  
  class B
  {
  public:
      class A
      {
       public:
          int member_a;
      
          A(int member);
          void calc(B&amp; b, int factor);
          std::thread thread_starting_wrapper(B&amp; b, int factor);
      };
  
      int member_b;
      std::thread myTread;
      A* myA;
  
      B();
      std::thread start_thread();
      std::thread start_thread_wrapper();
  };
  </code></pre>
  <p><strong>B.cpp</strong></p>
  <pre><code>#include &quot;B.h&quot;
  #include &lt;iostream&gt;
  
  
  B::B()
  {
      member_b = 10;
      myTread = std::thread(nullptr);
      myA = new A(5);
  }
  
  B::A::A(int member)
  {
      member_a = member;
  }
  
  
  void B::A::calc(B&amp; b, int factor)
  {
       std::cout &lt;&lt; (b.member_b + member_a) * factor &lt;&lt; std::endl;
  }
  
  std::thread B::start_thread()
  {
      return std::thread(&amp;B::A::calc, myA, std::ref(*this), 2);
      // return std::thread([this] { this-&gt;myA-&gt;calc(std::ref(*this), 2); } );
  }
  
  std::thread B::A::thread_starting_wrapper(B&amp; b, int factor)
  {
      return std::thread([&amp;] {calc(std::ref(b), factor);});
  }
  
  std::thread B::start_thread_wrapper()
  {
       return this-&gt;myA-&gt;thread_starting_wrapper(std::ref(*this), 2);
  }
  </code></pre>
  <p><strong>main.cpp</strong></p>
  <pre><code>#include &quot;B.h&quot;
  
  int main()
  {
      B* b = new B();
      std::thread t = b-&gt;start_thread();
      //std::thread t2 = b-&gt;start_thread_wrapper();
      t.join();
  }
  </code></pre>
  <p>Neither using the lambda version nor wrapping the function into A helps. What am I missing?</p>