views:

56

answers:

3

Can anybody help me with this simple code??

#include <iostream>
using namespace std;

void testFunction(){
    cout<<"This is the test function 0"<<endl;
}

void testFunction1(){
    cout<<"This is the test function 1"<<endl;
}

void testFunction2(){
    cout<<"This is the test function 2"<<endl;
}

void (*fp[])()={testFunction,testFunction1,testFunction2};

int main(){

    //fp=testFunction;
    (*fp[testFunction1])();
    //cout<<"Addrees of the function pointer is:"<<*fp;
}

I am getting the following error:

error: invalid types `void (*[3])()[void ()()]' for array subscript|
+2  A: 

I think you meant to write:

(*fp[1])();

That is, you index the array with an int rather than the function itself.

Kim Gräsman
+7  A: 

You're trying to use a function pointer as an array index. That won't fly, array indices must be integer.

To call through a function pointer, just call:

(*fp[1])();

or the (even shorter!)

fp[1]();

will work.

unwind
Thanks ...i don't know how i missed that.
Jonathan
+2  A: 

Your indexing the array fp of functions with a function pointer, try something like:

(*fp[some_index])();

instead

Andreas Brinck