anthony
anthony

Reputation: 902

Vector of void function pointers with parameters C++

I'm creating an application with a class that has a number of animation methods. I need these animation methods to be called in a random way. So, my idea was to create a vector of void function pointers, and iterate through the vector. I can't get it to compile though. I'm getting the error: "invalid use of void expression".

Applicable code:

.h

std::vector<void(*)(int,float)> animationsVector;
void setAnimations();
void circleAnimation01(int circleGroup, float time);

.cpp

polygonCluster01::polygonCluster01()
{
    setAnimations();
}

void polygonCluster01::setAnimations()
{
    animationsVector.push_back(circleAnimation01(1,2.0)); //error is here
}

void polygonCluster01::circleAnimation01(int circleGroup, float animLength)
{
    //other code
}

I've followed some other posts here which suggest I'm doing it right, but it still won't compile, and I'm not sure why.

Upvotes: 0

Views: 640

Answers (2)

Quentin
Quentin

Reputation: 63154

polygonCluster01::circleAnimation01 is not a free-standing function, but a member function. Thus, you need a member function pointer to store its adress. Here is the type you're looking for :

std::vector<void(polygonCluster01::*)(int,float)> animationsVector;
//               ^^^^^^^^^^^^^^^^^^

Edit: let's complete this answer.

When you give the correct type to your vector, it still won't compile. This is because, as stated by crashmstr, function pointers and member function pointers are just that - a pointer to a (member) function. In particular, they can't store parameters for later use, which you are trying to do.

So what you actually need is not a mere (member) function pointer, but something that can wrap a function and some parameters to call it later.

Well, C++11 has you covered ! Take a look at std::function. It's a type-erased container, designed to do just what is written above. You can use it like this :

std::vector<std::function<void(polygonCluster01*)>> animationsVector;

...

animationsVector.push_back(std::bind(
    &polygonCluster01::circleAnimation01, // Grab the member function pointer
    std::placeholders::_1, // Don't give a caller for now
    1, 2.0 // Here are the arguments for the later call
));

...

animationsVector[0](this); // Call the function upon ourselves

Upvotes: 5

Arkaitz Jimenez
Arkaitz Jimenez

Reputation: 23198

Your vector contains function pointers, not the result of the function you are calling in there.

animationsVector.push_back(circleAnimation01(1,2.0));

Use this instead

animationsVector.push_back(circleAnimation01);

The invalid use of void expression that you are getting is because you are trying to store the result of the circleAnimation01 function call which is void instead of a pointer to a function that returns void upon receiving an int and a float.

Also, as Quentin has stated, you need them to be functions, not member functions, either change the signature of the vector or change those members to free functions.

Upvotes: 0

Related Questions