gpt4 book ai didi

c++ - 在类内创建回调映射时出错

转载 作者:行者123 更新时间:2023-11-30 01:25:00 26 4
gpt4 key购买 nike

代码:

class Base{
enum eventTypes{ EVENT_SHOW };
std::map<int, boost::function<bool(int,int)> > m_validate;
virtual void buildCallbacks();
bool shouldShowEvent(int x, int y);
};
void Base::buildCallbacks(){
m_validate[ EVENT_SHOW ] = boost::bind(&Base::shouldShowEvent,this);
}

我收到以下错误:

 In base.cxx
return (p->*f_);
Error: a pointer to a bound function may only be used to call
the function (boundfuncalled)

我明白了错误的意思,除了调用有界成员函数外,我不能做任何其他事情,但我该如何避免这个问题?我不确定为什么这不起作用。

最佳答案

m_validate[ EVENT_SHOW ] = boost::bind(&Base::shouldShowEvent,this);

bind() 的调用产生一个不带参数的函数对象。您不能使用这样的对象来调用 Base::shouldShowEvent,因为它有两个参数。所以你必须把函数对象变成一个有两个参数的对象:

m_validate[ EVENT_SHOW ] = boost::bind(&Base::shouldShowEvent,this, _1, _2);

(未测试...)

关于c++ - 在类内创建回调映射时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13040406/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com