问题描述
所以我在AntiFlood类中有这个成员函数:
void AntiFlood::unBan()
{
QThread::msleep(5000);
std::string linetoPost = "KICK " + roomToPost +" "+ nickToPost + "\r\n";
sendIT(linetoPost);
}
,我想将其传递给: threadpool.globalInstance()-> start(unBan);
-
不起作用-结果错误:没有匹配的函数来调用'QThreadPool :: start()'threadpool.globalInstance()-> start(unBan); ^; 但另一方面,如果我使用lambda:
auto lam = [this,room,nick](){ QThread::msleep(5000); std::string linetoPost = "KICK " + roomToPost +" "+ nickToPost + "\r\n"; sendIT(linetoPost); }; threadpool.globalInstance()->start(lam);
工作正常。
如何将void AntiFlood :: unBan()传递给threadpool.globalInstance()-> start(),这需要std :: function
解决方法
您看到的基本问题是AntiFlood::unBan
是(或至少“似乎是”)非静态成员函数。在这种情况下,必须针对类型为AntiFlood
的有效对象调用它。由于QThreadPool::start
具有签名...
void QThreadPool::start(std::function<void ()> functionToRun,int priority = 0)
您需要为其传递一个“自包含” std::function<void()>
,这正是您要做的...
auto lam = [this,room,nick]()
{
QThread::msleep(5000);
std::string lineToPost = "KICK " + roomToPost +" "+ nickToPost + "\r\n";
sendIT(lineToPost);
};
threadpool.globalInstance()->start(lam);
通过在lambda中捕获this
。
简而言之,我想说的是您目前 正确/可接受的方式。