I know this question is a duplicate, however I could not find an answer on the other questions that helped my particular case. I should mention I am quite new to C++ and may be making a variety of mistakes that have nothing to do with the thread module. My goal here is just to have a lamda expression be run in a thread constantly, this lambda function would add one to a value every time spacebar is pressed. The program then checks this value to see if it is greater than 10, if it is, the program exits. The problem is that every time I try to build it (Using Visual Studio Community 2019), I get the following error:
'std::invoke': no matching overloaded function found
Failed to specialize function template 'unknown-type std::invoke(_Callable &&,_Types &&...) noexcept(<expr>)'
Here is my code:
#include <thread>
#include <iostream>
#include <Windows.h>
using std::thread;
using std::cout;
using std::endl;
int main()
{
//Value modified by the lambda expression, when it becomes greater than 10,
//the program will wait for the thread to finish and then exit.
int number = 0;
//Value to be checked by the lambda expression everytime the loop repeats,
//if it is false, the loop will break.
bool programRunning = true;
//Lambda expression to update number on screen when spacebar is pressed.
auto update = [](bool& isRunning, int& number) {
while (isRunning)
{
//If space is pressed
if (GetAsyncKeyState(VK_SPACE) != 0)
{
//Add one to the number, this is what the program checks to see if it should exit.
number++;
system("cls");
}
cout << "The current number is " << number << endl;
Sleep(500);
}
};
//Creating thread from lambda function and passing references of programRunning and number.
thread Updater(update, &programRunning, &number);
//Checks if the number modified by the lambda expression is greater than 10,
//if so, the program waits for the thread to finish and then exits.
while (true)
{
if (number > 10)
{
programRunning = false;
Updater.join();
exit(0);
}
else
{
Sleep(500);
}
}
}