你能帮我解决这个编译器错误吗?
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)也不能工作。
发布于 2010-12-01 00:20:46
您需要指定如何将func捕获到lambda中。
[]不会捕获任何内容
[&]按引用捕获
[=]按值捕获(复制)
T::ForEach([&](T *what) {我还建议您通过常量引用发送func。
static void ComputeGenericDropCount(const function<void(Npc *, int)>& func)https://stackoverflow.com/questions/4315862
复制相似问题