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:
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!
std::vector<std::function<void(int, bool, Move**)>>? Then use lambdas orstd::bindor similar tools to use specific objects.void (*)(int, bool, Move_t**)is the same typeGetMoveFunctionrefers to. You could write (for imho improved readability):using GetMoveFunction = void (*)(int, bool, Move_t**);instead of the typedef and it would mean the exact same thing. The type has nothing to do withvoid*; the brackets around*change the meaning.Game::getPawnMovesis a non-static member function. Therefore, the actual type of that function isvoid (Game::*)(int, bool, Move **), notvoid (*)(int, bool, Move **). The second error is just a side-show of misunderstanding the first error and stabbing at a solution.staticare not normal functions because they have an hidden argumentthisthat refers to the instanced object.