I have two functions called
void funcBlock(A a, B b, int C, long& R, long& M, double& D)void funcNormal(A a, B b, long& R, long& M, double& D)In main method, I would like to use the values from these two function by looping them such as,
main method:
combineFunc cfs[] = { funcBlock, funcNormal }
for (int idx = 0; idx < 2; idx++) //Cause it has two functions to loop over
{
combineFunc cf = cfs[idx];
//Do the rest of the stuffs
}
I encounter error :
error: cannot convert ‘void (*)(A, B, int, long int&, long int&, double&)’ to ‘combineFunc’ in initialization
How do i fix this?
This is impossible because your functions have different signatures (these are different types). C ++ does not support arrays of elements of different types. But you can match signatures by adding unused parameter, it does not change anything at all, but allows you to put them in one container:
typedef void(*combineFunc)(A, B, int, long int&, long int&, double&);
void funcBlock(A a, B b, int C, long& R, long& M, double& D);
void funcNormal(A a, B b, int unused, long& R, long& M, double& D);
combineFunc cfs[] = { funcBlock, funcNormal };
Another way is to use an array of structures that can store all your functions, like so
struct combineFunc
{
enum TYPE
{
NORMAL,
BLOCK
} type;
combineFunc(void(*nf)(A, B, int, long int&, long int&, double&)) :
type(NORMAL), nf_(nf)
{
}
combineFunc(void(*fb)(A, B, long int&, long int&, double&)) :
type(BLOCK), fb_(fb)
{
}
union
{
void(*nf_)(A, B, int, long int&, long int&, double&);
void(*fb_)(A, B, long int&, long int&, double&);
};
};
CombineFunc cfs[] = { funcBlock, funcNormal };
Your question is best answered with a question.
Within the loop, when you call cf(arguments), what arguments would you write between the parentheses? Would the arguments include C or not?
I think that, if you answer this question, you will see why what you want probably cannot be done until your program has been redesigned a bit. The redesign might be as simple, though, as letting funcNormal() accept (and ignore) a dummy argument in place of C.
If the last suggestion is what you want, then
void funcNormal(A a, B b, int, long& R, long& M, double& D)
Notice that the int, because ignored, is unnamed.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With