6

I want use a std::function in std::map, follow the code:

#include <functional>
#include <map>

class MyClass {
    ...
    std::function<void()> Callback();
    std::map<std::string, Callback> my_map;
    ...
}

std::map receive a Key and a T, but a not knew whats mistake in my code, him no access the std::map functions(insert, end, find...)

Using typedef, him run.But why std::function not run?

I firts place: whats the problem?

Before: How to solve it? --code sample please =D;

Thanks by help

user3854612
  • 127
  • 1
  • 1
  • 7

2 Answers2

15

You've declared Callback to be a function (returning std::function), not a type. You need a type to declare what you're storing in the map. I guess you want

typedef std::function<void()> Callback;
Mike Seymour
  • 249,747
  • 28
  • 448
  • 644
0

There is one more way of declaring map, having class function ptr.

  • If you don't want to use typedef.

    std::map<std::string, void (MyClass::*)()> my_map;

user438383
  • 5,716
  • 8
  • 28
  • 43