Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How Should I Define an Array of Pointers to Functions in C++?

I’m trying to make an array of functions so I can call functions from the array with an index. I can’t seem to figure out the parentheses, asterisks and brackets to create this array of functions. Here is what I have:

void Game::getPawnMoves(int position, bool color, Move ** moveList) {
    ...
}

typedef void (*GetMoveFunction) (int, bool, Move **);
void Game::getLegalMoves(Move ** moveList) {
    GetMoveFunction functions[] = 
    {
        getPawnMoves, 
        getKnightMoves, 
        getBishopMoves,
        getRookMoves,
        getQueenMoves,
        getKingMoves
    };
    ...
}

All of the getPawnMoves, getKnightMoves, and the rest all have the same arguments and all return void. Move is just a struct with two chars and an enum. In this case, if you’d like to try compiling it, you could replace Move ** with int **.

The error I’m getting when I compile is:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Game.cpp:443:5: error: cannot convert ‘Game::getPawnMoves’ from type 
‘void (Game::)(int, bool, Move**) {aka void (Game::)(int, bool, Move_t**)}’ to 
type ‘GetMoveFunction {aka void (*)(int, bool, Move_t**)}’

So for some reason the compiler thinks that GetMoveFunction is void * instead of void, but if I change the typedef to

typedef void (GetMoveFunction) (int, bool, Move **);

I get the error:

Game.cpp:435:28: error: declaration of ‘functions’ as array of functions
  GetMoveFunction functions[] = 

I’m honestly super stuck on this one. Thanks so much for your help!

Thanks for everyone’s help! Solution:

typedef void (Game::*GetMoveFunction) (int, bool, Move **);
void Game::getLegalMoves(Move ** moveList) {
    GetMoveFunction functions[] = 
    {
        &Game::getPawnMoves, 
        &Game::getKnightMoves, 
        &Game::getBishopMoves,
        &Game::getRookMoves,
        &Game::getQueenMoves,
        &Game::getKingMoves
    };
    ...
}

>Solution :

How Should I Define an Array of Pointers to Functions in C++?

typedef void (*GetMoveFunction) (int, bool, Move **);

This is a pointer to function.

GetMoveFunction functions[] = 
{
    // ...
};

This is an array of pointers to functions. You’ve achieved what you asked for in the title of the question.


Game.cpp:443:5: error: cannot convert ‘Game::getPawnMoves’ from type 
‘void (Game::)(int, bool, Move**) {aka void (Game::)(int, bool, Move_t**)}’ to 
type ‘GetMoveFunction {aka void (*)(int, bool, Move_t**)}’

The problem here is that you are trying to initialise the function pointers using non-static member functions. Function pointers cannot point to non-static member functions.

What you probably need is an array of pointers to member functions of Game instead. Another option is an array of type erasing function wrappers (std::function).

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading