How to Declare an array of function pointers?

I am attempting to create an array of function pointers. The examples I follow to do this are from:

Here is the syntax I came up with that I put in the class declaration of a header file:

double GetdCdK( double V );
double GetdCdn( double V );
double GetdCdVo( double V );
double GetdCdCo( double V );

// The below gets the error:
// include\junctioncap.h|142|error: cannot convert 'JunctionCap::GetdCdK' from type 'double (JunctionCap::)(double)' to type 'double (*)(double)'|
// for each of the three other functions also

double ( *jacobian[4] )( double ) = { GetdCdK, GetdCdn, GetdCdVo, GetdCdCo };

// The below gets the error:
// include\junctioncap.h|142|error: too many initializers for 'double (* [0])(double)'|
// In addition to the above errors.

double ( *jacobian[] )( double ) = { GetdCdK, GetdCdn, GetdCdVo, GetdCdCo };

What is wrong with this syntax? Can this be done in gcc?

The code you give is not an error in my compiler, C or C++, so I suspect they really are a different kind of function. Be careful with your const specifiers, you need to get them exactly right.

They're not function members, are they? You can't make function pointers to those, unless they're static. (And making them static will cut them off from class-local values.)

Those functions are class members. I did not know that pointers cannot be gotten for class member functions. That was the problem. Thanks. :slight_smile:

A function pointer does not contain enough information to point to a member function, it's missing the class instantiation ( the bit of memory containing member variables, etc) so C++ won't let you do it.

You can do it with static member functions because they don't even have access to a class' members. If that doesn't matter, if they really don't use the class, you can make them static and get pointers to them.

class myclass {
public:
        static int myfunction(int that) {
                return(42);
        }

};

int (*fn)(int) = myclass::myfunction;