【问题标题】:C++: Issues in creating threads; error C2672: 'std::invoke': no matching overloaded function foundC++:创建线程的问题;错误 C2672:“std::invoke”:找不到匹配的重载函数
【发布时间】:2017-05-01 06:09:29
【问题描述】:

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

我有两个类如下:

A.cpp:

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

  private:
         int value;
          // a lot of variables

}

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;
       }
}

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

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

          b->run();

          return 0;

       }

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

  error C2672: 'std::invoke': no matching overloaded function found

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

P.S:我正在使用以下包括:

 #include <thread>
 #include <iostream>

还有:

using namespace std;

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

【问题讨论】:

  • 那是很多空白......

标签: c++ multithreading visual-studio compiler-errors


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2020-09-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-01
    • 1970-01-01
    • 2018-04-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多