0

I'm building an application where the interface is like a shell. The idea is to write the function name and the function is called. I saw a post in stackoverflow (see below) that explains a way to do this storing the function pointer. I'm storing my functions in a map of type std::map<std::string, std::function<void(const std::vector<std:.string>, const std::map<std:.string, std::string>)> > but when I compile it gives the follwing error

error: could not convert ‘{{"exit", ((GitDeploy::Shell*)this)->GitDeploy::Shell::exit}, {"help", ((GitDeploy::Shell*)this)->GitDeploy::Shell::help}, {"dbinfo", ((GitDeploy::Shell*)this)->GitDeploy::Shell::dbinfo}, {"list", ((GitDeploy::Shell*)this)->GitDeploy::Shell::list}, {"create", ((GitDeploy::Shell*)this)->GitDeploy::Shell::create}, {"update", ((GitDeploy::Shell*)this)->GitDeploy::Shell::update}, {"remove", ((GitDeploy::Shell*)this)->GitDeploy::Shell::remove}}’ from ‘<brace-enclosed initializer list>’ to ‘const std::map<std::basic_string<char>, std::function<void(std::vector<std::basic_string<char> >, std::map<std::basic_string<char>, std::basic_string<char> >)> >’
         };

shell.h

#ifndef SHELL_CLASS
#define SHELL_CLASS
#include <string>
#include <map>
#include <functional>
#include <vector>

namespace GitDeploy {

    class Shell {
        private:
            std::map<std::string, std::function<void(const std::vector<std::string>, const std::map<std::string, std::string>)> > const methods {
                {"exit", exit},
                {"help", help},
                {"dbinfo", dbinfo},
                {"list", list},
                {"create", create},
                {"update", update},
                {"remove", remove}
            };

            std::map<std::string, std::string> const method_help {
                {"exit", "Exists GitDeploy"},
                {"help", "Prints available commands"},
                {"dbinfo", "Prints database information"},
                {"list", "Lists repositories."},
                {"create", "Adds a new repository"},
                {"update", "Updates a existing repositor\nyou must pass is uuid or partial uuid and the values to update\nupdate <uuid> path=<path> reset=<reset>"},
                {"remove", "Deletes a repository\nTo delete a repository, pass the uuids or partial uuids to delete\ndelete <uuid> <uuid> [...]"}
            };

            bool exit_shell;
        public:
            void serve();

            void exit(const std::vector<std::string>, const std::map<std::string, std::string>);
            void help(const std::vector<std::string>, const std::map<std::string, std::string>);
            void dbinfo(const std::vector<std::string>, const std::map<std::string, std::string>);
            void list(const std::vector<std::string>, const std::map<std::string, std::string>);
            void create(const std::vector<std::string>, const std::map<std::string, std::string>);
            void update(const std::vector<std::string>, const std::map<std::string, std::string>);
            void remove(const std::vector<std::string>, const std::map<std::string, std::string>);
    };
}

#endif

Answer to StackOverflow post

#include <iostream>
#include <map>

int add(int i, int j) { return i+j; }
int sub(int i, int j) { return i-j; }

typedef int (*FnPtr)(int, int);

int main() {
    // initialization:
    std::map<std::string, FnPtr> myMap;
    myMap["add"] = add;
    myMap["sub"] = sub;

    // usage:
    std::string s("add");
    int res = myMap[s](2,3);
    std::cout << res;
}

Thanks,

Hugo Rodrigues

  • The abuse of `eof` in that code and the pointless top-level qualifiers on function parameters worry me a great deal. – Kerrek SB Jan 18 '17 at 14:01
  • 3
    No, post a [mcve] as part of the question, instead of dumping a link to some external web site that can stop working at any time, rendering the question meaningless. Having said that, and without looking at some external site, it looks to me like these are not functions, but class methods, and the problem is very obvious. – Sam Varshavchik Jan 18 '17 at 14:03
  • I am worried about all those C casts. They are probably not correct. – nwp Jan 18 '17 at 14:03
  • 1
    FWIW, I usually go with a lambda that captures 'this' when wanting to save a member function as an object. – Erik Alapää Jan 18 '17 at 14:16
  • I'm starting with c++, thats why I have some issues in my code @SamVarshavchik the post was a Stack Overflow one, thats why I posted a link. I edited the question to fix it. Yes they are class methods, how can I do it to store the method on the map? – Hugo Rodrigues Jan 18 '17 at 14:27
  • The "entire project" link most definitely does not go to stackoverflow. – Sam Varshavchik Jan 18 '17 at 14:29
  • @SamVarshavchik Yes I know, it't going to GitHub, I thought that refering it may be helpfull – Hugo Rodrigues Jan 18 '17 at 14:31
  • It is not just helpful, it contains all the pertinent information that's needed to understand this question. That's the issue. It's going to get downvoted, until it's replaced by a [mcve]. I didn't make the rules. An external link is fine, but only if it's completely optional. – Sam Varshavchik Jan 18 '17 at 14:40
  • @SamVarshavchik Hmmm alright, sorry. I fix it – Hugo Rodrigues Jan 18 '17 at 16:46

1 Answers1

0

I fix it by converting the methods to static