I am porting a legacy C program to C++. However, the compiler is not happy and requires additional typecastings for C++. For example, I have this function…
void foreach_element_in_patch(Patch *patch, void (*func)(),
long arg1, long process_id);
In the original C Code, it is used like this…
foreach_element_in_patch( patch, display_interactions_in_element,
mode, process_id );
However, for C++ I need to typecast the second argument to stop the compiler from gernerating an error.
foreach_element_in_patch( patch, (void (*)())display_interactions_in_element,
mode, process_id );
The error generated by the compiler is the following…
invalid conversion from ‘void (*)(Patch*, long int, long int)’ to ‘void (*)()’
Now is there a way to ask the compiler not to generate errors for such things. I have tried prefixing this function with extern "C" but still the C++ compiler is not happy. My applications is loaded with such code and I do not have the time to adjust so much code.
The easiest thing to do is to change the function prototypes and headers to declare the correct type of function pointer. So for example, for
I just need to change it to
Instead of making changes at every function call.