【问题标题】:THREAD ERROR: invalid use of non-static member function [duplicate]线程错误:非静态成员函数的无效使用[重复]
【发布时间】:2017-05-19 11:13:21
【问题描述】:

我正在尝试理解 C++ 中的线程,但我不知道如何解决这个问题。

我想调用两个线程来运行名为“createS”的函数,但我得到了这个错误:

错误:非静态成员函数的使用无效

我已阅读有关此主题的其他问题,但我真的不明白如何使我的代码正常工作。

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

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

【问题讨论】:

  • 尝试将 CreateS() 类设为静态。

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


【解决方案1】:
 thread thread_1(&test_class::createS, this, 0, nCells/2, map1); 
 thread thread_2(&test_class::createS, this, nCells/2, nCells, map2);

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

【讨论】:

  • 当你没有参数时,你可以做thread thread_1(&amp;test_class::createS, this);
猜你喜欢
  • 2019-05-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-06-26
  • 1970-01-01
  • 2013-06-27
相关资源
最近更新 更多