'Code that compiles with gcc but not with g++
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.
Solution 1:[1]
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++.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | gha.st |
