我正在用c编写一个程序,但我需要使用c++库来使用 ADC。
在我的代码中,我有一个我编写的名为调度程序的库,该库使用gcc进行编译时没有错误,但是当我尝试使用g++进行编译时,出现错误:
scheduler.c:55:35: error: too many arguments to function call, expected 0, have 1
tasks[i].func(tasks[i].args);
Run Code Online (Sandbox Code Playgroud)
这是调度程序结构:
typedef struct _tasks_t
{
char *name;
unsigned int period; /**< Contains the period the task. If it's 0 it doesn't execute the task */
void (*func)(); /**< Contains a pointer to the function of the task */
bool args_on; /**< true if the function has arguments */
void *args; /**< pointer to function args */
} tasks_t;
Run Code Online (Sandbox Code Playgroud)
以下是生成错误的行:
/** Goes through every task to check if the time passed is >= to the period and if the period is != 0 */
for (i = 0; i < tasks_size; i++)
{
if ((time_ms - t_last[i] >= tasks[i].period) && (tasks[i].period != 0))
{
t_last[i] = time_ms; /** Saves the "new" last time that the task was executed */
if (tasks[i].args_on)
{ /** Executes the task function */
tasks[i].func(tasks[i].args);
}
else
{
tasks[i].func();
}
}
}
Run Code Online (Sandbox Code Playgroud)
[编辑]:我通过编写解决了问题void (*func)(void *),现在我在数组或结构的 for 中传递函数参数。
void (*func)()当解释为 C 时,是指向具有未指定参数的函数的函数指针,而当解释为 C++ 时,是指向具有0 个参数的函数的函数指针(相当于void (*func)(void)C 中的函数)。
tasks[i].func(tasks[i].args);用一个参数调用它,这在 C++ 中是无效的。