I have been attempting a sort of text-based command interface for a virtual board game. I've been reading on member functions as parameters, but all my attempts so far have not produced results. My intent was to be modular so that I can easily add commands by setting a string to call them by in the console.
This is the function I use to call for each function:
void command(string in, string name, void (*fnc)())
{
if(in == name)
{
(*fnc)();
}
}
I am calling this in my play() function:
void play()
{
string cmd;
do
{
getline(cin, cmd);
command(cmd, "print_all", &print_all);
}
while(cmd != "exit");
}
print_all():
void print_all()
{
for(int i = 0; i < cards.size(); i++)
{
cout << cards[i].name
<< "/n Mana:" << cards[i].cost
<< "/n Attack:" << cards[i].attack
<< "/n Defense:" << cards[i].defense
<< endl;
}
}
Here is the constructor, as to provide more information on the code:
public:
game()
{
init_cards(); // This just fills the vector "cards" with data.
play();
}
And here is my main:
int main()
{
game GAME;
return 0;
}
For some reason, this is producin this error:
error: ISO C++ forbids taking the address of an unqualified or parenthesized non-static member function to form a pointer to member function. Say '&game::print_all' [-fpermissive]|
I have tried replacing the call parameter with &game::print_all
, as recommended by the error log, but to no effect.
Thank you for any help in advance. Hopefully, it's a simple fix and I'm just too blind to see it.