【发布时间】:2018-02-09 06:28:52
【问题描述】:
亲爱的读者
我正在为进程调度程序制作一个库模块。默认调度程序有 8 个条目 0..7,调度程序中的函数/进程使用函数指针调用。编写为普通的 C 程序,它按预期工作。但是现在我想在 .h 文件中配置函数指针,我有指向函数指针的编译器错误。
我对位于 proclist[8] 中 &scheduler::dummy 的函数指针有疑问,它们会产生错误,我不知道如何解决它们。
谁能告诉我该怎么做。
产生的错误是
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();
};
【问题讨论】:
-
指向成员函数的指针与指向非成员函数的指针不同。最大的区别是成员函数需要一个对象来调用,而非成员函数不需要。了解
std::function、std::bind和lambda expressions。 -
这个错误告诉你你的函数签名是错误的以及它所期望的。 “(调度程序::)(uint8_t)”。之后调用该函数是另一回事,“一些程序员老兄”已经提供了答案。
-
一种选择是制作
scheduler::nan和scheduler::dummy静态成员函数(或全局函数)。如果这不可能,那么您必须按照上面 cmets 中描述的行重新设计您的代码。 -
我试图修正你的格式。我不确定它是否和你的原版一模一样,但至少类型现在是可见的。