C++ freeRTOS 任务,非静态成员函数的无效使用 [英] C++ freeRTOS Task, invalid use of non-static member function

查看:64
本文介绍了C++ freeRTOS 任务,非静态成员函数的无效使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题出在哪里?

void MyClass::task(void *pvParameter){
     while(1){
         this->update();
     }
}

void MyClass::startTask(){
    xTaskCreate(this->task, "Task", 2048, NULL, 5, NULL);
}

但是,我明白了:

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

error: invalid use of non-static member function

我找不到任何有用的文档来检查错误在哪里,
但我认为应该是这样的:(C++11 的 std::thread)例如:

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) e.g.:

xTaskCreate(&MyClass::task, "Task", 2048, (void*)this, 5, NULL);

适合我的解决方案:

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

推荐答案

我将此模式与包装函数一起使用,以使用非静态成员函数实例化 pthread.xTask 中调用的函数是静态成员函数,使用 void* 指针调用任务函数.我的类.hpp:

I use this pattern with a wrapper function for instanciating pthread with non-static member functions. The function called in xTask is a static member function, calling the task function using void* pointer. MyClass.hpp :

class MyClass {
    public:
        MyClass() {}
        ~MyClass() {}
    private:
        void update();
        void task();
        static void startTaskImpl(void*);
        void startTask();
 }

MyClass.cpp :

MyClass.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);
}

这篇关于C++ freeRTOS 任务,非静态成员函数的无效使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆