Compiler error C3493: 'func' cannot be implicitly captured because no default capture mode has been specified

Can you help me resolve this compiler error?

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

I can't understand why it's giving me the error and I don't know how to fix it. ComputeGenericDropCount(auto func) doesn't work either.

Answers


You need to specify how to capture func into the lambda.

[] don't capture anything

[&] capture-by-reference

[=] capture-by-value (copy)

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

I'd also recommend that you should send func by const reference.

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

Need Your Help

Android Webview how to remove focused/hovered state of an element on load?

android webview focus hover

I have an Android app using webview to load and display html pages.

iterating over multi dimensional array for a menu

php arrays codeigniter multidimensional-array

Hello I have a menu made in codeigniter. but I also want this to have submenu's

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.