THREAD ERROR: invalid use of non-static member function [duplicate]
Asked Answered
B

1

16

I'm trying to understand threads in C++ but I don't know how to solve this problem.

I want to call two threads to run the function called "createS" but I get this error:

error: invalid use of non-static member function

I've read other questions about this topic but I really don't understand how to make my code works.

Could someone explain me what I'm doing wrong and try to help my find a solution?

test_class.cpp

void test_class::generateS(){

     map1=new multimap<double,vector<int>>;
     map2=new multimap<double,vector<int>>;

     thread thread_1( createS, 0, nCells/2, map1 ); 
     thread thread_2( createS, nCells/2, nCells, map2);

     thread_1.join();
     thread_2.join();
}

void test_class::createS(int startP, int endP, Costs *mapPointer){
     //i do some stuff
}

test_class.h

void createS(int start, int end, Costs *mapPointer);
void generateS();
Billhook answered 5/1, 2017 at 1:45 Comment(1)
Try making CreateS() class static.Coolie
A
21
 thread thread_1(&test_class::createS, this, 0, nCells/2, map1); 
 thread thread_2(&test_class::createS, this, nCells/2, nCells, map2);

Note: if createS does not depend on object state, better make it a static class member and call the way you did.

Akela answered 5/1, 2017 at 2:1 Comment(1)
When you don't have parameters you can just do thread thread_1(&test_class::createS, this);Scandalmonger

© 2022 - 2024 — McMap. All rights reserved.