2017-04-01 73 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); 
} 
相關問題