你能帮我解决这个编译器错误吗?
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/