1

I have a std map that combines a string with a function pointer like:

 std::map<std::string, void (*)()> funcs {
       {"print", &h::print},
       {"scan", &h::scan_cmd},
       {"connect", &h::stream},
       {"stream", &h::stream}
 };

where h is the enclosing class in which this map has been initialized:

class h {
public:
   void print();
   void scan();
   void connect();
   void stream();
   std::map<std::string, void (*)()> funcs {
           {"print", &h::print},
           {"scan", &h::scan_cmd},
           {"connect", &h::stream},
           {"stream", &h::stream}
     };
};

I get this error:

No matching constructor for initialization of 'std::map<std::string, void (*)()>' (aka 'map<basic_string<char>, void (*)()>')

I've also tried puttting the map in this form:

std::map<std::string, void (*)()> funcs;
funcs["print"] = &print;
funcs["scan"] = &scan_cmd;
funcs["connect"] = &stream;
funcs["stream"] = &stream;

But then I got this error:

Size of array has non-integer type ' const char [6]'

I'm not exactly sure where the problem is - my guess is that it's with the void (*) () portion. I'm sure this is a c++ 11 compiler.

4
  • Are you sure you have a C++11 compiler? Also, your code is broken around "connect" (mismatched quotation marks?). Commented Feb 20, 2019 at 20:44
  • And please show the definition of h and of its type. Commented Feb 20, 2019 at 20:45
  • @KerrekSB I've updated it - thanks! Commented Feb 20, 2019 at 20:49
  • (void connect(); void stream();…"connect", &h::stream}, looks funny as does funcs["connect"] = &stream;.) Commented Feb 20, 2019 at 21:19

1 Answer 1

1
void (*)()

is pointer to ordinary function which takes no arguments and returns no value. In your example print, stream, scan_cmd are non-static member functions of h class. Syntax to define pointer to member functions of h class looks like

void (h::*)()

Try:

   std::map<std::string, void (h::*)()> funcs {
           {"print", &h::print},
           {"connect", &h::stream},
           {"stream", &h::stream}
     };
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.