I am trying to use the async function to run multiple processes on a large csv file at once to avoid long wait times for the user however I am getting the error:
no instance of overloaded function "async" matches the argument list
I have googled around and not found anything that fixed it and am out of ideas and as I am quite new to coding C++ any help would be greatly appreciated! I have included all my code below.
#include "stdafx.h"
#include <cstring>
#include <fstream>
#include <iostream>
#include <string>
#include <algorithm>
#include <future>
using namespace std;
string token;
int lcount = 0;
void countTotal(int &lcount);
void menu()
{
int menu_choice;
//Creates the menu
cout << "Main Menu:\n \n";
cout << "1. Total number of tweets \n";
//Waits for the user input
cout << "\nPlease choose an option: ";
cin >> menu_choice;
//If stack to execute the needed functionality for the input
if (menu_choice == 1) {
countPrint(lcount);
} else { //Validation and invalid entry catcher
cout << "\nPlease enter a valid option\n";
system("Pause");
system("cls");
menu();
}
}
void countTotal(int &lcount)
{
ifstream fin;
fin.open("sampleTweets.csv");
string line;
while (getline(fin, line)) {
++lcount;
}
fin.close();
return;
}
void countPrint(int &lcount)
{
cout << "\nThe total amount of tweets in the file is: " << lcount;
return;
}
int main()
{
auto r = async(launch::async, countTotal(lcount));
menu(); //Starts the menu creation
return 0;
}
The line
auto r = async(launch::async, countTotal(lcount));
doesn't do what you think it does. It immediately invokes and evaluates countTotal(lcount), which returns void. The code is therefore invalid.
Look at the documentation for std::async. It takes a Callable object. The easiest way to produce a Callable for countTotal and to defer execution is using a lambda:
auto r = async(launch::async, []{ countTotal(lcount); });
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With