C++函数调用缺少参数列表;使用 '&Runner::runTask' 创建指向成员的指针
C++ function call missing argument list; use '&Runner::runTask' to create a pointer to member
这个问题似乎在SO之前已经回答了,但是尽管看了其他解决方案,我仍然无法弄清楚为什么我得到错误:
函数调用缺少参数列表;使用'&Runner::runTask'创建指向成员
的指针
我有一个类Runner
,它将负责调度任务,以异步地在单独的线程上运行任何子工作。
在我的跑步者的start
方法中,我有以下代码:
void start(const bool runTaskAsync = true)
{
if(!isRunning()) return;
running = true;
if(runTaskAsync)
{
Worker = std::thread(runTask, this);
}
else
{
this->runTask();
}
}
编译器不喜欢的麻烦行是:Worker = std::thread(runTask, this);
。根据给出的错误(以及本网站提出的其他问题),我尝试做以下操作
Worker = std::thread(&Runner::runTask);
然而,我仍然得到相同的错误。runTask
方法是Runner
类上的私有方法,定义为:
void runTask()
{
while(isRunning())
{
// this_thread refers to the thread which created the timer
std::this_thread::sleep_for(interval);
if(isRunning())
{
// Function is a public method that we need to call, uses double parens because first calls the function called Function
// and then the second set of parens calls the function that the calling Function returns
Function()();
}
}
}
对Function()()
的调用调用传递给Runner
实例的模板函数,task
的runner私有成员变量签名为std::function<void(void)> task;
, Function()()
的实现签名为:
const std::function<void(void)> &Function() const
{
return task;
}
调用时(据我所知)将运行Function()
,然后运行task()
如果还需要其他细节,请让我知道。我目前没有实例化Runner
的任何实例,我只是将Runner.h
包含在我的main.cpp
文件中,看看它是否会编译。
根据给出的错误(以及本网站提出的其他问题),我试图做以下事情)
Worker = std::thread(&Runner::runTask);
应该是:
Worker = std::thread(&Runner::runTask, this);
每个非静态成员函数都接受一个隐式的this
,当您想将该成员函数传递给std::thread
时,它是公开的(并且是必需的)
这应该能奏效:
Worker = std::thread(&Runner::runTask, this);
相关文章:
- C++-我可以创建另一个类的成员并在构造函数中使用它吗
- int数据类型的指针指向的是什么,如果是一个类的私有数据成员,我们创建了该类的两个对象?
- 使用成员在类中创建 lambda 表达式
- 使用带有 ref 参数的成员函数创建线程时出现编译错误
- C++ 双向链表:使用矩阵类成员创建和填充列表
- C++:将向量传递到构造函数以创建成员变量的最佳方法?
- 从类指针的向量创建成员变量的向量
- 根据模板参数条件 C++17 创建成员别名
- 为原始指针、智能指针和值创建成员函数的编译时变体
- C :如何声明std ::函数而无需创建成员函数以分配给它们
- VS2017 非标准语法。使用 ' &' 创建成员指针
- 根据C 中的变量值创建成员函数
- 在C++中创建成员函数模板专用化
- 如果成员对象是父类的一部分,则无法创建成员对象
- 如何通过元编程正确创建成员模板函数指针数组
- 需要帮助创建c++成员函数来表示任务与RTOS microcos III
- 在可变模板函数中创建成员函数指针元组
- 如何在实例和静态作用域之间创建成员变量
- 将使用typedef创建成员吗?
- 内部类——c++,内部类,以及为什么你只能创建成员作为指向它们的指针