mardi 3 novembre 2015

I am trying to make a hashmap of of string functions

I am trying to make a map which stores a string as an identifier and a function that returns a string i have tried typedef but i kept running into problems because i couldn't convert my typedef string (command)() to a regular string i have also tried map commands but it gives me an expression expected error but it does work if i replace string with int. Does anybody know a way of doing this? This is what my code looks like

        #include "iostream"
        #include <map>
        #include <functional>


        using namespace std;

        class GameController {

        public:
            void inputReader();

        private:


            bool gameOver = false;
            map<string,string(*)()> commands;//Does not work

            //commands
            string  commandReader(string* inputCommand);
            void initCommands();

            //both
            char* end();
            string run();

            //while attacking
            string attack();
            string usePotion();
            string useItem();

            //while in room
            string engage();
            string searchRoom();
            string rest();
            string checkBag();
            string checkMap();
            string checkStats();
            //string save();



        };



        #endif //ROGUE_GAMECONTROLLER_H

    #include "GameController.h"

    GameController::GameController(){
        initCommands();
    }
void GameController::inputReader() {

while (!gameOver){
    string x;
    getline(cin,x);
    cout << commandReader(&x) << endl;
    }

}

string  GameController::commandReader(string *inputCommand) {

    for (map<string,string>::iterator it = commands.begin(); it!=commands.end(); ++it)
    {
        if(it->first == *inputCommand)
        {
            return it->second;
        }

    }
    return "Computer says no type help for commands";
}

    void GameController::initCommands() {


        commands["end"] = end;
        //while attacking
        commands["run"] = run;
        commands["attack"] = attack;
        commands["use potion"] = usePotion;
        commands["use item"] = useItem;

        //while in room
        commands["engage"] = engage;//TODO
        commands["search"] = searchRoom;
        commands["rest"] = rest;
        commands["check bag"] = checkBag;
        commands["map"] = checkMap;
        commands["stats"] = checkStats;
    }

Aucun commentaire:

Enregistrer un commentaire