我有以下设置:
typedef std::function<void()> reaction;
class Node
{
public:
...
private:
void connect();
void receive();
private:
const std::map<std::pair<Status, Event>, reaction> TransTable = {
{{DISCONNECTED, CONNECT}, &Node::connect},
{{CONNECTING, RECEIVE}, &Node::receive}
};
}
但我总是得到错误:
error: could not convert from <brace-enclosed initializer list> to const std::map<std::pair<Status, Event>, std::function<void()> >
我的initalizer列表有什么问题?
答案 0 :(得分:3)
您的问题缺少MCVE,但错误消息足够清晰:reaction
似乎是std::function<void()>
的typedef。诸如&Node::connect
之类的成员函数指针无法转换为std::function<void()>
,因为后者缺少任何实际调用该函数的this
参数。
但是,您可以使用lambda来捕获和存储当前正在初始化此TransTable
成员的实例:
const std::map<std::pair<Status, Event>, reaction> TransTable = {
{{DISCONNECTED, CONNECT}, [this] { connect(); }},
{{CONNECTING, RECEIVE}, [this] { receive(); }}
};