c++ - 指向类成员的函数指针生成编译器错误

标签 c++ arrays class pointers

亲爱的读者
我正在为进程调度程序制作一个库模块。默认情况下,调度程序有 8 个条目 0..7,调度程序中的函数/进程使用函数指针调用。作为普通 C 程序编写,它按预期工作。但是现在我想在 .h 文件中配置函数指针我有指向函数指针的编译器错误。 我在 proclist[8] 中位于 &scheduler::dummy 的函数指针有问题,它们会给我带来错误,我不知道如何解决它们。

Can some one advice me what to do.

产品的错误是

scheduler.cpp:9: In file included from

Error compiling libraries

scheduler.h: 56:87: error:
cannot convert 'void (scheduler::*)(uint8_t*) {aka void (scheduler::*)(unsigned char*)}' to 'void (*)(uint8_t*) {aka void (*)(unsigned char*)}' in initialization 
           { 7, 3900  ,10000 ,0,PROC_ACT ,&scheduler*: dummy7 , &proclist[7].val ,65  ,1}}; 
\\process table entry 7
Build failed for project 'Scheduler'

并且是这个错误的 7 倍。

scheduler.h: 56:87: error: 
 cannot convert 'void (scheduler::*)(uint8_t*) {aka void (scheduler::*)(unsigned char*)}' to 'void (*)(uint8_t*) {aka void (*)(unsigned char*)}' in initialization

.h文件中的代码如下
(第 56 行在代码块中指示)

 //process structure
typedef struct  process_t{                                  // Start of the Process scheduler structure
    uint8_t                         index               ;   // index of this process in the  process structure array
    uint32_t                        starttime           ;   // Absolute start and next call time of the function
    uint32_t                        delta               ;   // Time between the function to be called
    uint32_t                        exetime             ;   // Time it takes to execute the function
    uint8_t                         stat                ;   // skip, delete, change, active
    void                          (*pt2function)(uint8_t*) ;// Pointer to function to be called
    uint8_t                        *valptr              ;   // Pointer to value given as function Parameter
    uint8_t                         val                 ;   // Default value being pointed at
    uint8_t                         nextprocess         ;   // Index to next process in the process structure array
};

class scheduler {

    public:
            void    run();
            void    man  (uint8_t *); 
            void    dummy(uint8_t *);


        //  Processes 0,1..5 arrays of Process structs.
            process_t proclist[8]   =   {{0, ROLLOFFSET ,0     ,0,PROC_ACT ,&scheduler::man     , &proclist[0].val ,INIT    ,1} ,   //Initialise(), Run ones in the process list
                                        { 1, 3000       ,2237  ,0,PROC_ACT ,&scheduler::dummy   , &proclist[1].val ,65      ,2} ,   //process table entry 1
                                        { 2, 3100       ,2718  ,0,PROC_ACT ,&scheduler::dummy   , &proclist[2].val ,66      ,3} ,   //process table entry 2  
                                        { 3, 3200       ,3141  ,0,PROC_ACT ,&scheduler::dummy   , &proclist[3].val ,67      ,4} ,   //process table entry 3 
                                        { 4, 3300       ,2237  ,0,PROC_SKP ,&scheduler::dummy   , &proclist[4].val ,65      ,5} ,   //process table entry 4
                                        { 5, 3400       ,2718  ,0,PROC_SKP ,&scheduler::dummy   , &proclist[5].val ,66      ,6} ,   //process table entry 5
                                        { 6, 3500       ,3141  ,0,PROC_SKP ,&scheduler::dummy   , &proclist[6].val ,67      ,7} ,   //process table entry 6
/*===Line 56 ==>*/                      { 7, 3900       ,10000 ,0,PROC_ACT ,&scheduler::dummy   , &proclist[7].val ,65      ,1}};   //process table entry 7

            // and other functions if needed

    private:
            int8_t   n, cnt;
            uint32_t    mmillis();
};

最佳答案

非静态成员函数有一个指向类实例的隐藏指针this,所以Pointers to Member Functions需要一个对象,您不能像普通函数指针那样使用它们。

根据您的设计,您可以相互

  1. 使方法静态化:

    static void    man  (uint8_t *); 
    static void    dummy(uint8_t *);
    

    也许您必须再添加一个参数来传递对 schedulerprocess_t 实例的引用。

  2. 或将pt2function更改为指向成员函数的指针:

    class scheduler;
    
    //process structure
    struct  process_t{                            // Start of the Process scheduler structure
        // ...
        void  (scheduler::*pt2function)(uint8_t*) ;// Pointer to function to be called
        // ...
    

    您以后可以像这样从 scheduler 使用这些指向成员函数的指针:

    class scheduler {
    // ...
    void run() {    
        (this->*proclist[0].pt2function)(proclist[0].valptr);
    }
    

Playground

关于c++ - 指向类成员的函数指针生成编译器错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48699960/

相关文章:

javascript - 努力比较边数以确定形状类型

java - 如何在不多次执行该方法的情况下将返回值传递给其他类

python - 为什么我的嵌套 python 类实例变成了元组?

c++ - CreateThread 似乎表现得像 fork()

c++ - boost.test header 之间的区别

c++ - 即使片段 alpha 为 1.0,合成器也会混合 OpenGL

c - c 中的 fgets 和 strlen

c++ - 字符串和函数对象

arrays - 将数组中的可选值赋给 nil

javascript - 模仿 Array.from 方法的静态方法在 Javascript 中不起作用