2017-04-01 74 views
0

我只是想将一个lambda函数传递给一个回调函数。我正在使用std::function进行回调。我需要传递数据到这个函数,这是我遇到问题的地方。下面的错误代码中的代码“无法转换为预期类型”。目标是在SDL的事件中使用回调。我不确定这是否是正确的做法。我将回调函数存储在unordered_map中,密钥为SDL_Event.typevectorstd::function对于SDL事件回调,使用lambda错误的std :: function

我在设置中调用了事件轮询中的dispatch()subscribe。在subscribe()拉姆达

// main.cpp 
window->subscribe(SDL_KEYDOWN, [](SDL_Event& ev) -> void { 
    std::cout << "key pressed" << std::endl; 
}); 

// eventhandler.cpp 
void EventHandler::subscribe(int _event, std::function<void(const SDL_Event&)> _callback) 
{ 
    m_callbacks[_event].push_back(_callback); 
} 

回答

0

取得了非常愚蠢的错误的[]出现的错误...参数不匹配。下面是正确的代码。即我在lambda中没有const ...

window->subscribe(SDL_KEYDOWN, [](const SDL_Event& ev) -> void { 
    std::cout << "key pressed" << std::endl; 
}); 

void EventHandler::subscribe(int _event, std::function<void(const SDL_Event&)> _callback) 
{ 
    m_callbacks[_event].push_back(_callback); 
} 
相关问题