首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >编译器错误C3493:无法隐式捕获“func”,因为尚未指定默认捕获模式

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

Stack Overflow用户
提问于 2010-12-01 00:14:27
回答 1查看 26.6K关注 0票数 38

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

代码语言:javascript
复制
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)也不能工作。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2010-12-01 00:20:46

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

[]不会捕获任何内容

[&]按引用捕获

[=]按值捕获(复制)

代码语言:javascript
复制
T::ForEach([&](T *what) {

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

代码语言:javascript
复制
static void ComputeGenericDropCount(const function<void(Npc *, int)>& func)
票数 66
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/4315862

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档