std :: list包含std :: function [英] std::list containing std::function

查看:394
本文介绍了std :: list包含std :: function的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要实现的是包含std::functionsstd::list.我正在尝试实现一个回调系统,在该系统中可以将函数添加到列表中,然后可以循环遍历列表并调用每个函数.

What I'm trying to achieve is std::list that contains std::functions. I'm trying to implement a callback system where functions can be added to the list and then the list can be looped through and each function called.

我在A类课上有以下内容:

What I have in class A is:

std::list<std::function<void( bool )>> m_callbacks_forward;

bool registerForward( std::function<void(bool)> f ) { m_callbacks_forward.push_back ( f ); return true; };

void GameControllerHub::invokeForward( bool state ) {

    for( std::list<std::function<void(bool)>>::iterator f = m_callbacks_forward.begin(); f != m_callbacks_forward.end(); ++f ){

        f();
    }

}

然后在B类中

std::function<void(bool)> f_move_forward = [=](bool state) {
        this->moveForward(state);
    };

    getGameController()->registerForward ( f_move_forward );

...

bool Player::moveForward( bool state ) {
...
}

但是在GameControllerHub::invokeForward中我得到了错误

类型'std :: list> :: iterator'(也称为'__list_iterator')不提供呼叫运营商"

"type 'std::list >::iterator' (aka '__list_iterator') does not provide a call operator"

我正在使用Xcode编译

I'm compiling with Xcode

推荐答案

您需要取消引用迭代器以获取其引用的元素.迭代器本身没有operator().

You need to dereference the iterator to get the element it referes to. The iterator itself doesn't have operator().

编写(*f)(state);,或者,如果需要,编写f->operator()(state);

Write (*f)(state); or, if you wish, f->operator()(state);

这篇关于std :: list包含std :: function的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆