dimanche 30 mai 2021

unresolved overloaded function type>, int, main()::

I don't know how to pass the method with a lambda parameter into the std::thread. My code sample as below:

using namespace std;
#include <bits/stdc++.h>
#include <iostream>
#include <string>
#include <thread>     
template<typename var>
void show(int a, var pf)
{
    for(int i = 0; i < 10; pf(i))
    {
        cout << "i = " << i << endl;
    }
}

int main()
{
    int int_test = 10;
    auto func = [](int &x)->int{ return x = x + 1; };
    show(10, func);
    std::thread a(&show, 10, func);
    a.join();
}

Compile with the command: g++ ThreadLambda.cpp -pthread -std=c++11 -o test;

And the error show:

ThreadLambda.cpp:149:66: error: no matching function for call to ‘std::thread::thread(<unresolved overloaded function type>, int, main()::<lambda(int&)>)’
     std::thread a(&show, 10, [](int &x)->int{ return x = x + 1; });

Aucun commentaire:

Enregistrer un commentaire