std::函数,带有 SDL 事件回调的 lambda 错误
std::function with lambda error for SDL event callbacks
我只想将lambda函数传递给函数进行回调。我正在使用std::function
进行回调。我需要将数据传递给这个函数,这就是我遇到问题的地方。下面的代码错误说"无法转换为预期类型"。目标是对具有 SDL
的事件使用回调。我不确定这是否是正确的方法。我将回调函数存储在一个unordered_map
中,键是SDL_Event.type
和std::function
vector
。
我在事件轮询中调用dispatch()
,并在设置中调用subscribe
。错误发生在subscribe()
lambda 的[]
中
// 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);
}
犯了
非常愚蠢的错误...参数不匹配。正确的代码如下。即我没有 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);
}
相关文章:
- MSVC __debugbreak() 与 openGL 错误回调一起使用时不会产生调用堆栈
- 调用 java 的回调() 时应用程序崩溃.由于 detatchThread 而获得运行时错误
- 通过实用程序 fn 将捕获的 lambda 传递给 C 样式回调 - 错误
- 更新 Visual Studio 2017,现在出现编译错误 C7510:"回调":使用依赖模板名称必须以 'template' 为前缀
- 调用某个回调函数两次会导致分段错误:Nan
- 从C++代码抛出错误调用 JavaScript 回调函数 - nbind
- 回调方法显示错误,类型为"void(*)"的值不能用于初始化类型的实体
- OpenGL 调试 错误回调函数的 ID 参数是什么?
- 从触摸回调访问向量元素的方法时,不良访问错误
- 插件中节点.js/Nan 回调C++不频繁的隔离错误
- std::函数,带有 SDL 事件回调的 lambda 错误
- clang消毒剂回调错误
- 带有回调函数的cocos2d-x菜单项错误
- 通过功能指针通过DLL进行回调功能,运行时检查失败#0错误
- pybind11基本回调,不兼容的功能签名错误
- C 11 std :: bind and auto的编译错误,用于回调函数参数
- C++ - 在 C++ 中实现回调时的允许错误
- 调用带参数的方法时出现本机回调错误
- 回调函数,aerospike_query_foreach给出构建错误
- cocos2d-x添加触摸事件回调错误