用GCC编译的代码,但不使用G++
我正在用 c 编写一个程序,但是我需要使用 c ++ 库与ADC一起使用。
库编译没有 gcc 的错误
scheduler.c:55:35: error: too many arguments to function call, expected 0, have 1
tasks[i].func(tasks[i].args);
在我的代码中,我有一个名为Scheduler的库,此 调度程序结构:
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;
这是生成错误的行:
/** 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();
}
}
}
[edit]:我通过编写void( *func)(void *)
解决了问题阵列或结构。
I am writing a program in c, but i need to use a c++ library to work with an ADC's.
In my code I have a library that I wrote called scheduler, this library compiles with no errors with gcc but when I try to compile with g++ I get the error:
scheduler.c:55:35: error: too many arguments to function call, expected 0, have 1
tasks[i].func(tasks[i].args);
Here is the scheduler struct:
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;
And here are the lines that are generating the error:
/** 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();
}
}
}
[EDIT]: I solved the problem by writing void (*func)(void *)
and now I pass function arguments in the for of an array or a struct.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
void(*func)()
是用 intemified 参数的函数指针,当解释为C时,但是使用 0 < 0 <的函数指针/em>参数,当解释为c ++(等效于void(*func)(void)
在c中)。tasks [i] .func(tasks [i] .args);
用一个参数调用,这在C ++中无效。void (*func)()
is a function pointer to a function with unspecified arguments, when interpreted as C, but a function pointer to a function with 0 arguments, when interpreted as C++ (equivalent tovoid (*func)(void)
in C).tasks[i].func(tasks[i].args);
calls it with one argument, which is invalid in C++.