I'm programming a Discrete Event Simulator (DES) where events will be user programmed functions. I have a Class called UserEvents and a private member function called Add
void Add(const void *Parameters, void *ReturnValue);
I'm trying to make a vector that would store the following struct:
typedef struct FunctionPointerAlias { std::string FAlias; void(UserEvents::*FPointer)(const void*, void*); };
Based on a string, I want to call different functions. Thus switch would be nice but switch can't handle strings (and I don't want to hash my strings). So I made a vector with a struct (string Alias, FunctionPointer) and then I'll iterate over my vector looking for the user's inputed string, recovering the corresponding function pointer and calling it.
All user functions should be of type void, receiving parameters and returning values through void* pointers as such:
void UserEvents::Add(const void *Parameters, void *ReturnValue)
{
int *A = (int*)Parameters;
int *B = A;
B++;
int *C = (int*)ReturnValue;
*C = *A + *B;
//memcpy(ReturnValue, &C, sizeof(int));
return;
}
(TL;DR) The problem lies when I try to call the function pointed by the function pointer. I get the error "Expression preceding parentheses of apparent call must have (pointer-to-) function type" on the line right above return in the "Add" if.
void UserEvents::Choose(const std::string Alias, const void *Parameters, void *ReturnValue)
{
if (Alias == "Example")
{
// *ReturnValue = Function(Parameters)
return;
}
if (Alias == "Add") {
void (UserEvents::*FunctionPointer)(const void*, void*) = &UserEvents::Add;
(*FunctionPointer)(Parameters, ReturnValue);
return;
}
}
Add
meant to be a member function or not? If not, why is it? If so, how are you expecting to invoke it without an object on which to invoke it? – David SchwartzPlay
function to play nothing in particular. A class member functions makes some particular class instance do something. There has to be some class instance. – David Schwartz