用GCC编译的代码,但不使用G++

发布于 2025-01-28 20:34:09 字数 1410 浏览 2 评论 0原文

我正在用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

摇划花蜜的午后 2025-02-04 20:34:09

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 to void (*func)(void) in C).

tasks[i].func(tasks[i].args); calls it with one argument, which is invalid in C++.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文