【问题标题】:Compiler error C3493: 'func' cannot be implicitly captured because no default capture mode has been specified编译器错误 C3493:“func”无法被隐式捕获,因为没有指定默认捕获模式
【发布时间】:2011-05-17 23:35:04
【问题描述】:

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

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) 也不起作用。

【问题讨论】:

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


【解决方案1】:

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

[] 不捕获任何内容

[&amp;] 引用捕获

[=] 按值捕获(复制)

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

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

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

【讨论】:

  • 这不起作用并给我同样的错误;我还认为[] 的意思是“不捕获任何东西”; [&amp;] 表示“通过引用捕获所有上值”,[*] 表示“通过复制捕获所有上值”,等等。但我认为这只会影响上值的捕获方式,即它只会影响 内部的代码 拉姆达
  • 您需要捕获 func 才能在 lambda 中使用它,您可以这样做。我不明白你怎么会得到同样的错误。我回家后自己试试。
  • 对不起!正如你所说,我将&amp; 添加到了错误的lambda,来自ComputeGenericDropCount 的回调,而不是ForEach。现在一切都说得通了:它当然需要知道如何捕获函数!!再次感谢。
  • &amp; 是我需要的。谢谢
猜你喜欢
  • 1970-01-01
  • 2015-07-24
  • 2018-12-26
  • 2015-10-28
  • 2021-06-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-06-18
相关资源
最近更新 更多