编译器错误C3493:无法隐式捕获'func',因为未指定默认捕获模式

Tho*_*ini 38 c++ compiler-errors visual-studio-2010 visual-studio c++11

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

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; });
}
Run Code Online (Sandbox Code Playgroud)

我不明白为什么它给我错误,我不知道如何解决它.ComputeGenericDropCount(auto func)也不起作用.

ron*_*nag 65

您需要指定如何捕获func到lambda.

[] 不捕捉任何东西

[&] 捕获按引用

[=] 按值捕获(复制)

T::ForEach([&](T *what) {
Run Code Online (Sandbox Code Playgroud)

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

static void ComputeGenericDropCount(const function<void(Npc *, int)>& func)
Run Code Online (Sandbox Code Playgroud)