添加源文件头文件至工程
.c及其对应的.h文件
编译环境
keil 5.39 MDK6
编译错误
无法找到size_t
添加#include <stddef.h> /*size_t*/
ceventRun报错
修改后还是报以下错误怎么解决:
../User/cpost_cevent/cevent.c(118): error: redefinition of 'function' with a different type: 'void (*)(void *, void *)' vs 'void (*)(void *)'
118 | void (*function)(void *, void *) = (void (*)(void *, void *))(cevent->param[0]);
| ^
../User/cpost_cevent/cevent.c(114): note: previous definition is here
114 | void (*function)(void *) = (void (*)(void *))(cevent->param[0]);
| ^
../User/cpost_cevent/cevent.c(119): error: too many arguments to function call, expected 1, have 2
119 | function((void *)cevent->param[1], (void *)cevent->param[2]);
| ~~~~~~~~ ^~~~~~~~~~~~~~~~~~~~~~~~
../User/cpost_cevent/cevent.c(122): error: redefinition of 'function' with a different type: 'void (*)(void *, void *, void *)' vs 'void (*)(void *)'
122 | void (*function)(void *, void *, void*) = (void (*)(void *, void *, void*))(cevent->param[0]);
| ^
../User/cpost_cevent/cevent.c(114): note: previous definition is here
114 | void (*function)(void *) = (void (*)(void *))(cevent->param[0]);
| ^
../User/cpost_cevent/cevent.c(123): error: too many arguments to function call, expected 1, have 3
123 | function(cevent->param[1], cevent->param[2], cevent->param[3]);
| ~~~~~~~~ ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
../User/cpost_cevent/cevent.c(126): error: redefinition of 'function' with a different type: 'void (*)(void *, void *, void *, void *)' vs 'void (*)(void *)'
126 | void (*function)(void *, void *, void*, void*) = (void (*)(void *, void *, void*, void*))(cevent->param[0]);
| ^
../User/cpost_cevent/cevent.c(114): note: previous definition is here
114 | void (*function)(void *) = (void (*)(void *))(cevent->param[0]);
| ^
../User/cpost_cevent/cevent.c(127): error: too many arguments to function call, expected 1, have 4
127 | function(cevent->param[1], cevent->param[2], cevent->param[3], cevent->param[4]);
| ~~~~~~~~ ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
../User/cpost_cevent/cevent.c(130): error: redefinition of 'function' with a different type: 'void (*)(void *, void *, void *, void *, void *)' vs 'void (*)(void *)'
130 | void (*function)(void *, void *, void*, void*, void*) = (void (*)(void *, void *, void*, void*, void*))(cevent->param[0]);
| ^
../User/cpost_cevent/cevent.c(114): note: previous definition is here
114 | void (*function)(void *) = (void (*)(void *))(cevent->param[0]);
| ^
../User/cpost_cevent/cevent.c(131): error: too many arguments to function call, expected 1, have 5
131 | function(cevent->param[1], cevent->param[2], cevent->param[3], cevent->param[4],
| ~~~~~~~~ ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
132 | cevent->param[5]);
| ~~~~~~~~~~~~~~~~
../User/cpost_cevent/cevent.c(135): error: redefinition of 'function' with a different type: 'void (*)(void *, void *, void *, void *, void *, void *)' vs 'void (*)(void *)'
135 | void (*function)(void *, void *, void*, void*, void*, void*) = (void (*)(void *, void *, void*, void*, void*, void*))(cevent->param[0]);
| ^
../User/cpost_cevent/cevent.c(114): note: previous definition is here
114 | void (*function)(void *) = (void (*)(void *))(cevent->param[0]);
| ^
../User/cpost_cevent/cevent.c(136): error: too many arguments to function call, expected 1, have 6
136 | function(cevent->param[1], cevent->param[2], cevent->param[3], cevent->param[4],
| ~~~~~~~~ ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
137 | cevent->param[5], cevent->param[6]);
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
../User/cpost_cevent/cevent.c(140): error: redefinition of 'function' with a different type: 'void (*)(void *, void *, void *, void *, void *, void *, void *)' vs 'void (*)(void *)'
140 | void (*function)(void *, void *, void*, void*, void*, void*, void*) = (void (*)(void *, void *, void*, void*, void*, void*, void*))(cevent->param[0]);
| ^
../User/cpost_cevent/cevent.c(114): note: previous definition is here
114 | void (*function)(void *) = (void (*)(void *))(cevent->param[0]);
| ^
../User/cpost_cevent/cevent.c(141): error: too many arguments to function call, expected 1, have 7
141 | function(cevent->param[1], cevent->param[2], cevent->param[3], cevent->param[4],
| ~~~~~~~~ ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
142 | cevent->param[5], cevent->param[6], cevent->param[7]);
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
../User/cpost_cevent/cevent.c(144): error: cannot jump from switch statement to this case label
144 | default:
| ^
../User/cpost_cevent/cevent.c(114): note: jump bypasses variable initialization
114 | void (*function)(void *) = (void (*)(void *))(cevent->param[0]);
| ^
../User/cpost_cevent/cevent.c(139): error: cannot jump from switch statement to this case label
139 | case 8:
| ^
../User/cpost_cevent/cevent.c(114): note: jump bypasses variable initialization
114 | void (*function)(void *) = (void (*)(void *))(cevent->param[0]);
| ^
../User/cpost_cevent/cevent.c(134): error: cannot jump from switch statement to this case label
134 | case 7:
| ^
../User/cpost_cevent/cevent.c(114): note: jump bypasses variable initialization
114 | void (*function)(void *) = (void (*)(void *))(cevent->param[0]);
| ^
../User/cpost_cevent/cevent.c(129): error: cannot jump from switch statement to this case label
129 | case 6:
| ^
../User/cpost_cevent/cevent.c(114): note: jump bypasses variable initialization
114 | void (*function)(void *) = (void (*)(void *))(cevent->param[0]);
| ^
../User/cpost_cevent/cevent.c(125): error: cannot jump from switch statement to this case label
125 | case 5:
| ^
../User/cpost_cevent/cevent.c(114): note: jump bypasses variable initialization
114 | void (*function)(void *) = (void (*)(void *))(cevent->param[0]);
| ^
../User/cpost_cevent/cevent.c(121): error: cannot jump from switch statement to this case label
121 | case 4:
| ^
../User/cpost_cevent/cevent.c(114): note: jump bypasses variable initialization
114 | void (*function)(void *) = (void (*)(void *))(cevent->param[0]);
| ^
../User/cpost_cevent/cevent.c(117): error: cannot jump from switch statement to this case label
117 | case 3:
| ^
../User/cpost_cevent/cevent.c(114): note: jump bypasses variable initialization
114 | void (*function)(void *) = (void (*)(void *))(cevent->param[0]);
| ^
更改如下:需要自己根据具体函数参数个数和函数类型去配置
/**
* @brief cevent运???
*
* @param cevent cevent
*
*/
static void ceventRun(CEvent *cevent)
{
if (cevent->paramNum < 1)
{
return;
}
void (*function)() = (void (*)())(cevent->param[0]);
switch (cevent->paramNum)
{
case 1:
function();
break;
/*后面的需要用再添加*/
// case 2:
// function(cevent->param[1]);
// break;
// case 3:
// function(cevent->param[1], cevent->param[2]);
// break;
// case 4:
// function(cevent->param[1], cevent->param[2], cevent->param[3]);
// break;
// case 5:
// function(cevent->param[1], cevent->param[2], cevent->param[3], cevent->param[4]);
// break;
// case 6:
// function(cevent->param[1], cevent->param[2], cevent->param[3], cevent->param[4],
// cevent->param[5]);
// break;
// case 7:
// function(cevent->param[1], cevent->param[2], cevent->param[3], cevent->param[4],
// cevent->param[5], cevent->param[6]);
// break;
// case 8:
// function(cevent->param[1], cevent->param[2], cevent->param[3], cevent->param[4],
// cevent->param[5], cevent->param[6], cevent->param[7]);
// break;
default:
break;
}
}
代码终极修复
ok,编译通过
static void ceventRun(CEvent *cevent)
{
if (cevent->paramNum < 1)
{
return;
}
void (*function)() = (void (*)())(cevent->param[0]);
switch (cevent->paramNum)
{
case 1:{
function();
break;
}
/*后面的需要用再添加*/
case 2:{
void (*function2)(void *) = (void (*)(void *))(cevent->param[0]);
function2((void *)cevent->param[1]);
break;
}
case 3:{
void (*function3)(void *, void *) = (void (*)(void *, void *))(cevent->param[0]);
function3((void *)cevent->param[1], (void *)cevent->param[2]);
break;
}
case 4:{
void (*function4)(void *, void *, void*) = (void (*)(void *, void *, void*))(cevent->param[0]);
function4((void *)cevent->param[1], (void *)cevent->param[2], (void *)cevent->param[3]);
break;
}
case 5:{
void (*function5)(void *, void *, void*, void*) = (void (*)(void *, void *, void*, void*))(cevent->param[0]);
function5((void *)cevent->param[1], (void *)cevent->param[2], (void *)cevent->param[3], (void *)cevent->param[4]);
break;
}
case 6:{
void (*function6)(void *, void *, void*, void*, void*) = (void (*)(void *, void *, void*, void*, void*))(cevent->param[0]);
function6((void *)cevent->param[1], (void *)cevent->param[2], (void *)cevent->param[3], (void *)cevent->param[4],
(void *)cevent->param[5]);
break;
}
case 7:{
void (*function7)(void *, void *, void*, void*, void*, void*) = (void (*)(void *, void *, void*, void*, void*, void*))(cevent->param[0]);
function7((void *)cevent->param[1], (void *)cevent->param[2], (void *)cevent->param[3], (void *)cevent->param[4],
(void *)cevent->param[5], (void *)cevent->param[6]);
break;
}
case 8:{
void (*function8)(void *, void *, void*, void*, void*, void*, void*) = (void (*)(void *, void *, void*, void*, void*, void*, void*))(cevent->param[0]);
function8((void *)cevent->param[1], (void *)cevent->param[2], (void *)cevent->param[3], (void *)cevent->param[4],
(void *)cevent->param[5], (void *)cevent->param[6], (void *)cevent->param[7]);
break;
}
default:{
break;
}
}
}
或者替换成下面更统一的方式:
/**
* @brief cevent运???
*
* @param cevent cevent
*
*/
static void ceventRun(CEvent *cevent)
{
if (cevent->paramNum < 1)
{
return;
}
switch (cevent->paramNum)
{
case 1:
{
void (*function)() = (void (*)())(cevent->param[0]);
function();
break;
}
case 2:
{
void (*function)(const void *) = (void (*)(const void *))(cevent->param[0]);
function(cevent->param[1]);
break;
}
case 3:
{
void (*function)(const void *, const void *) = (void (*)(const void *, const void *))(cevent->param[0]);
function(cevent->param[1], cevent->param[2]);
break;
}
case 4:
{
void (*function)(const void *, const void *, const void *) = (void (*)(const void *, const void *, const void *))(cevent->param[0]);
function(cevent->param[1], cevent->param[2], cevent->param[3]);
break;
}
case 5:
{
void (*function)(const void *, const void *, const void *, const void *) =
(void (*)(const void *, const void *, const void *, const void *))(cevent->param[0]);
function(cevent->param[1], cevent->param[2], cevent->param[3], cevent->param[4]);
break;
}
case 6:
{
void (*function)(const void *, const void *, const void *, const void *,
const void *) = (void (*)(const void *, const void *, const void *, const void *,
const void *))(cevent->param[0]);
function(cevent->param[1], cevent->param[2], cevent->param[3], cevent->param[4],
cevent->param[5]);
break;
}
case 7:
{
void (*function)(const void *, const void *, const void *, const void *,
const void *, const void *) = (void (*)(const void *, const void *, const void *, const void *,
const void *, const void *))(cevent->param[0]);
function(cevent->param[1], cevent->param[2], cevent->param[3], cevent->param[4],
cevent->param[5], cevent->param[6]);
break;
}
case 8:
{
void (*function)(const void *, const void *, const void *, const void *,
const void *, const void *, const void *) = (void (*)(const void *, const void *, const void *, const void *,
const void *, const void *, const void *))(cevent->param[0]);
function(cevent->param[1], cevent->param[2], cevent->param[3], cevent->param[4],
cevent->param[5], cevent->param[6], cevent->param[7]);
break;
}
default:
break;
}
}