1

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.

Artylo
  • 81
  • 6
  • There are not enough information to can give an answer. First of all, this functions looks to be member function(from the error code you've posted). It is this the case? – Heto May 28 '16 at 14:25
  • All functions are members of my class "game". in main() I only define the object with "game GAME;". All other functions are called in the constructor. – Artylo May 28 '16 at 14:30
  • ok, the issue is simple: I've posted the answer with code example on "duplicate post". I don't think they are duplicated. Your issue is different because everything is happening in member functions. – Heto May 28 '16 at 14:36
  • It's solves your issue? – Heto May 28 '16 at 14:47
  • Thank you, sir! You are a scholar and a saint. Do you mind if I ask you why this needs to be done in order to facilitate it as a usable void parameter? – Artylo May 28 '16 at 14:47
  • if you really want to understand, I think this FAQ can explain better than I can in this comment section: https://isocpp.org/wiki/faq/pointers-to-members – Heto May 28 '16 at 14:50
  • Ah, thank you. I will read through it. – Artylo May 28 '16 at 14:53

0 Answers0