Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++: Issues in creating threads; error C2672: 'std::invoke': no matching overloaded function found

Before marking this as duplicate, I have already seen the other answers and they did not solve my issue.

I have two classes as follows:

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

And then I have a simple main.cpp:

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

          b->run();

          return 0;

       }

I am trying to create two threads by calling the method run(). However, when I try to compile I get the following error:

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

I checked the other posts but nothing seemed to work for me. Any help would be appreciated.

P.S: I am using the following includes:

 #include <thread>
 #include <iostream>

and also:

using namespace std;

I am using other includes but they are irrelevant

like image 870
Mostafa Abdulrazic Avatar asked May 01 '17 06:05

Mostafa Abdulrazic


1 Answers

B::simulate is a non-static member function so it requires 2 parameters - this and ptr, while you supplying only one. You should redeclare it as static since it does not access this class members anyway.

like image 149
user7860670 Avatar answered Oct 21 '22 05:10

user7860670