编译器错误C3493:无法隐式捕获“func”,因为未指定默认捕获模式 [英] Compiler error C3493: 'func' cannot be implicitly captured because no default capture mode has been specified

查看:2165
本文介绍了编译器错误C3493:无法隐式捕获“func”,因为未指定默认捕获模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您能帮我解决这个编译器错误吗?

  template< class T& 
static void ComputeGenericDropCount(function< void(Npc *,int)> func)
{
T :: ForEach([](T * what){
Npc * npc = Npc :: Find(what-> sourceId);

if(npc)
func(npc,what-> itemCount); //<< <错误这里
//错误1错误C3493:'func'不能隐式捕获,因为没有指定默认捕获模式

});
}

static void PreComputeNStar()
{
// ...
ComputeGenericDropCount< DropSkinningNpcCount>([](Npc * npc,int i ){npc-> nSkinned + = i;});
ComputeGenericDropCount< DropHerbGatheringNpcCount>([](Npc * npc,int i){npc-> nGathered + = i;})
ComputeGenericDropCount< DropMiningNpcCount>([](Npc * npc,int i){npc-> nMined + = i;})
}



我不明白为什么它给我的错误,我不知道如何解决它。 ComputeGenericDropCount(auto func)也无效。

解决方案

需要指定如何将 func 捕获到lambda中。



[] 不捕获任何内容



[&]

[=] 按值捕获(复制)

  T :: ForEach([&](T * what){

我还建议您通过const引用发送 func

  static void ComputeGenericDropCount(const function< void(Npc *,int)>& func)


Can you help me resolve this compiler error?

template<class T>
static void ComputeGenericDropCount(function<void(Npc *, int)> func)
{
    T::ForEach([](T *what) {
        Npc *npc = Npc::Find(what->sourceId);

        if(npc)
            func(npc, what->itemCount); // <<<<<<< ERROR HERE
            // Error    1   error C3493: 'func' cannot be implicitly captured because no default capture mode has been specified

    });
}

static void PreComputeNStar()
{
     // ...
    ComputeGenericDropCount<DropSkinningNpcCount>([](Npc *npc, int i) { npc->nSkinned += i; });
    ComputeGenericDropCount<DropHerbGatheringNpcCount>([](Npc *npc, int i) { npc->nGathered += i; });
    ComputeGenericDropCount<DropMiningNpcCount>([](Npc *npc, int i) { npc->nMined += i; });
}

I can't understand why it's giving me the error and I don't know how to fix it. ComputeGenericDropCount(auto func) doesn't work either.

解决方案

You need to specify how to capture func into the lambda.

[] don't capture anything

[&] capture-by-reference

[=] capture-by-value (copy)

T::ForEach([&](T *what) {

I'd also recommend that you should send func by const reference.

static void ComputeGenericDropCount(const function<void(Npc *, int)>& func)

这篇关于编译器错误C3493:无法隐式捕获“func”,因为未指定默认捕获模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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