Tyler
Tyler

Reputation: 13

Retrieving function pointers from container

I think this will make more sense if I put the code first:

I have a member variable defined as such:

std::queue<void (*)()> fptrs;

...and am trying to pop a pointer back out of it like this:

void (*f)() = fptrs.pop();

The 'alert' I get is this:

Assigning to 'void (*)()' from incompatible type 'void'

It seems that I'm not putting function pointers into my queue, or they're popping back out magically transformed. Either way, I'm not sure how to fix this, and I've fiddled with it for a bit.

Thanks for the help.

Upvotes: 1

Views: 86

Answers (2)

BlackMamba
BlackMamba

Reputation: 10254

#include <iostream>
#include <queue>
using namespace std;

typedef void (*pFun)();

void fun1(){ printf("fun1\n"); }
void fun2(){ printf("fun2\n"); }
int main()
{
    queue<pFun> d;
    d.push(fun1);
    d.push(fun2);

    pFun fun = d.front();
    fun();
}

Upvotes: 0

juanchopanza
juanchopanza

Reputation: 227418

std::queue::pop() returns void. You need a call to std::queue::front(), followed by one to std::queue::pop().

I would suggest using a typedef for the function pointer, for the sake of readability.

Upvotes: 1

Related Questions