QtConcurrent 与成员函数

我创建了一个 QFuture,我想用它来并行化对成员函数的调用.更准确地说,我有一个带有 .h 的 solveParallel 类:

I create a QFuture that I want to use to parallelize calls to a member function. More precisely, I have a class solveParallel with .h :

class solverParallel {
public:
  solverParallelData(Manager* mgr_);
  virtual ~solverParallel(void);

  void runCompute(solveModel * model_);

  bool resultComput();

private:
  Manager *myMgr;
  QFuture<bool> myFutureCompute;
}; 

runCompute() 方法正在创建 myFutureCompute 成员..cpp 看起来像

where the methode runCompute() is creating the myFutureCompute member. .cpp looks like

solveParallel::solveParallel(Manager* mgr_)
:m_mgr(mgr_)
{
}

solverParallel::~solverParallel(void){}

void solverParallel::runCompute(solveModel* model)
{
  futureComput = QtConcurrent::run(&this->myMgr,&Manager::compute(model));
}

bool solverParallelData::resultComput()
{
  return m_futureComput.result();
}

包含都可以.编译失败,上线

Include(s) are all right. Compilation fails, on line

futureComput = QtConcurrent::run(&this->myMgr,&Manager::compute(model));

出现此错误:

Error   44  error C2784: 'QFuture<T> QtConcurrent::run(T (__cdecl *)(Param1),const     Arg1 &)' : could not deduce template argument for 'T (__cdecl *)    (Param1)' from 'Manager **'   solverparallel.cpp 31

此外,关于同一行代码中&Manager"的鼠标信息:错误:非静态成员引用必须与特定对象相关.

In addition, on mouse info for '&Manager' in same line of code stands: Error: a nonstatic member reference must be relative to a specific object.

你知道诀窍在哪里吗?感谢和问候.

Do you see where is the trick? Thanks and regards.

推荐答案

来自 官方文档 :

QtConcurrent::run() 也接受指向成员函数的指针.这第一个参数必须是常量引用或指向类的实例.在以下情况下通过 const 引用传递很有用调用 const 成员函数;通过指针传递对于调用修改实例的非常量成员函数.

QtConcurrent::run() also accepts pointers to member functions. The first argument must be either a const reference or a pointer to an instance of the class. Passing by const reference is useful when calling const member functions; passing by pointer is useful for calling non-const member functions that modify the instance.

您正在传递一个指向指针的指针.另请注意,您不能按照自己的方式传递参数,而是作为 run 函数中的额外参数.以下应该有效:

You are passing a pointer to a pointer. Also notice that you cannot pass the arguments the way you do, but as extra arguments in the run function. The following should work:

futureComput = QtConcurrent::run(this->myMgr,&Manager::compute, model);

相关文章