c++ - 线程错误 : invalid use of non-static member function

标签 c++ multithreading compiler-errors non-static

<分区>

我想了解 C++ 中的线程,但我不知道如何解决这个问题。

我想调用两个线程来运行名为“createS”的函数,但出现此错误:

error: invalid use of non-static member function

我读过关于这个主题的其他问题,但我真的不明白如何让我的代码工作。

有人可以向我解释我做错了什么并尝试帮助我找到解决方案吗?

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();

最佳答案

 thread thread_1(&test_class::createS, this, 0, nCells/2, map1); 
 thread thread_2(&test_class::createS, this, nCells/2, nCells, map2);

注意:如果createS 不依赖于对象状态,最好将其设为static 类成员并按照您的方式调用。

关于c++ - 线程错误 : invalid use of non-static member function,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41476077/

相关文章:

c++ - Boost::Variant and function_types in it: How to put functions into Boost::variant?

java - ReentrantReadWriteLock:ReadLock和WriteLock有什么区别?

C++ 'class' 类型重定义错误

c - XC8 错误 224 : illegal # directive (first line)

c++ - 是否可以从现有基类动态分配派生类所需的额外内存?

c++ - 为模板化类型确定正确谓词的方法

ios - 如果我们在后台队列中调用 main.async 代码什么时候执行?

java - 如何跨服务器可靠地杀死@Scheduled 线程?

f# - Mono 编译器中的错误 : Files in libraries or multiple-file applications must begin with a namespace or module declaration

c++ - 需要帮助理解 "{}-initializer syntax minimizes the chances for unfortunate conversions"