vc++ thread constructor throwing compiler error c2672

50 views Asked by At

I have two identical C++ classes that are instantiated from within a lambda which is run in a separate thread.

These classes' objects are used from within another class X:

This is how these classes are used:

class A : public C
{
  std::thread thr;
  void thrFunction(X &x)
  {
    while(atomic<bool>){
      //long running process using members of C
    }
  }

  public:
    A();
    int start(X &x)
    {
      thr = std::thread(&A::thrFunction, this, std::ref(x)); //Throwing Error C2672 at compile-time
      return 0;
    }
};

class B : public C
{  
  void thrFunction(X &x)
  {
    while(atomic<bool>){
      //long running process using members of C
    }
  }

  public:
    B();
    int start(X &x)
    {
      thrFunction(x); //blocking call
      return 0;
    }
};

class X
{
    A *a;
    B *b;

  public:
    X();
    void cleanup();
    int callMembers()
    {
      a->start(*this);
      b->start(*this);
    }
};

int main()
{
  X *api = new X();
  auto lbda = [](X *api)
  {
    api->callMembers(); //meant to block here
    api->cleanup();
  }
  std::thread(lbda, api);

  while(true)
  {
    sleep(10);
  }
}

This code doesn't compile and throws this error:

C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.39.33519\include\thread(60,14): error C2672: std::invoke': no matching overloaded function found

The error is due to the thread constructor call in class A.

I have tried to fix it by searching for a solution on the Internet, but to no avail. Almost all of the solutions suggested are changing the thread instantiation syntax, but nothing has worked so far.

After some changes, the compiler error changes to:

C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.39.33519\include\memory(3434,35): error C2661: 'std::tuple<void (__cdecl A::* )(X &),A *,X>::tuple': no overloaded function takes 3 arguments

No matter what I try, I cannot get it to compile.

Maybe there is something related to the class hierarchy which is causing this error? That is why I shared the whole structure of the involved classes and members.

Can someone please help me resolve this error?

This got resolved. The actual line causing error was the lambda thread. I was not passing all the arguments.

Thanks for your time.

0

There are 0 answers