簡體   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