繁体   English   中英

用 gcc 但不能用 g++ 编译的代码

[英]Code that compiles with gcc but not with g++

我正在用c编写程序,但我需要使用c++库来处理 ADC。

在我的代码中,我编写了一个名为 scheduler 的库,该库使用gcc编译时没有错误,但是当我尝试使用g++编译时出现错误:

scheduler.c:55:35: error: too many arguments to function call, expected 0, have 1
                    tasks[i].func(tasks[i].args);

这是调度程序结构:

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();
            }
        }
    }

[编辑]:我通过编写void (*func)(void *)解决了这个问题,现在我在数组或结构的 for 中传递函数参数。

void (*func)()在解释为 C 时是指向具有未指定参数的函数的函数指针,但在解释为 C++ 时是指向具有0个参数的函数的函数指针(等效于void (*func)(void) C)。

tasks[i].func(tasks[i].args); 用一个参数调用它,这在 C++ 中是无效的。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM