C++:创建线程的问题;错误 C2672:“std::invoke”:找不到匹配的重载函数

Mos*_*zic 6 c++ multithreading compiler-errors visual-studio

在将其标记为重复之前,我已经看到了其他答案,但它们没有解决我的问题。

我有两个类如下:

A.cpp:

class A
{
  public:
          A();
          int getValue()//just an example of a get method 
               {
                     return value;
               }

  private:
         int value;
          // a lot of variables

}
Run Code Online (Sandbox Code Playgroud)

B.cpp:

class B
{ 
     public: 
            B();
            void addData(string fileName)
              {
                  A* a = new A();

                  //reads the file with the fileName and does alot of stuff
                 //after calculation is over it adds the object to the vector
                list.push_back(a);

              }

           void run()
             {
                 thread t1(&B::simulate, list[0]);
                 thread t2(&B::simulate, list[1]);

                 t1.join();
                 t2.join();
             }

   private:

     vector<A*> list;

     void simulate(A* ptr)
       {
            int value = 0;

            cout << "At first value is " << value << endl;
            weight = ptr->getValue();
            cout << "Then it becomes " << value << endl;
       }
}
Run Code Online (Sandbox Code Playgroud)

然后我有一个简单的 main.cpp:

  int main()
      {
          B* b = new B();
          b->addData("File1.txt");
          b->addData("File2.txt");

          b->run();

          return 0;

       }
Run Code Online (Sandbox Code Playgroud)

我试图通过调用 run() 方法来创建两个线程。但是,当我尝试编译时,出现以下错误:

  error C2672: 'std::invoke': no matching overloaded function found
Run Code Online (Sandbox Code Playgroud)

我检查了其他帖子,但似乎对我没有任何作用。任何帮助,将不胜感激。

PS:我使用的包括:

 #include <thread>
 #include <iostream>
Run Code Online (Sandbox Code Playgroud)

并且:

using namespace std;
Run Code Online (Sandbox Code Playgroud)

我正在使用其他包含但它们无关紧要

use*_*670 4

B::simulate是一个非静态成员函数,因此它需要 2 个参数 -thisptr,而您只提供一个。您应该将其重新声明为静态,因为它无论如何都不会访问this类成员。