[英]C++ freeRTOS Task, invalid use of non-static member function
Where is the Problem?问题出在哪里?
void MyClass::task(void *pvParameter){
while(1){
this->update();
}
}
void MyClass::startTask(){
xTaskCreate(this->task, "Task", 2048, NULL, 5, NULL);
}
But, I get this:但是,我明白了:
error: invalid use of non-static member function错误:无效使用非静态成员函数
I cannot find any useful doc to check where is the mistake,我找不到任何有用的文档来检查错误在哪里,
but i think that should be something like: (C++11's std::thread) eg:但我认为应该是这样的:(C++11 的 std::thread)例如:
xTaskCreate(&MyClass::task, "Task", 2048, (void*)this, 5, NULL);
solution that works for me:适合我的解决方案:
void MyClass::task(){
while(1){
this->update();
}
}
static void MyClass::startTaskImpl(void* _this){
static_cast<MyClass*>(_this)->task();
}
void MyClass::startTask(){
xTaskCreate(this->startTaskImpl, "Task", 2048, this, 5, NULL);
}
I use this pattern with a wrapper function for instanciating pthread with non-static member functions.我将此模式与包装函数一起使用,以使用非静态成员函数实例化 pthread。 The function called in xTask is a static member function, calling the task function using void* pointer. xTask中调用的函数是静态成员函数,使用void*指针调用任务函数。 MyClass.hpp:我的类.hpp:
class MyClass {
public:
MyClass() {}
~MyClass() {}
private:
void update();
void task();
static void startTaskImpl(void*);
void startTask();
}
MyClass.cpp:我的类.cpp:
void MyClass::task(){
while(1){
this->update();
}
}
void MyClass::startTaskImpl(void* _this){
(MyClass*)_this->task();
}
void MyClass::startTask(){
xTaskCreate(this->startTaskImpl, "Task", 2048, this, 5, NULL);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.