c++ - 编译器错误 C3493 : 'func' cannot be implicitly captured because no default capture mode has been specified

标签 c++ visual-studio visual-studio-2010 c++11 compiler-errors

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

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; });
}

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

最佳答案

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

[] 不捕获任何东西

[&] 引用捕获

[=] 按值捕获(复制)

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

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

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

关于c++ - 编译器错误 C3493 : 'func' cannot be implicitly captured because no default capture mode has been specified,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4315862/

相关文章:

c++ - 使用 operator+ 而不会泄漏内存?

c++ - 通过继承函数读取基类的私有(private)成员(?)

c++ - 当算法需要零秒/毫秒时间时,如何获得近似时间?

c# - 在 VS2010 中添加引用

c# - 具有关系的动态 Linq "Where statement"

visual-studio-2010 - TFS 警报未显示在警报资源管理器中

c++ - 快速像素访问 opencv

visual-studio-2010 - VS2010 是否支持 Windows 移动项目?

c++ - 编译器错误 C2275

c++ - FFmpeg 潜在的内存泄漏行为点。有这样的吗?