Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

No Instance of Overload Function "async" Matches the argument list

Tags:

c++

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;
}
like image 244
Kai Jones Avatar asked Nov 16 '25 01:11

Kai Jones


1 Answers

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); });
like image 98
Vittorio Romeo Avatar answered Nov 18 '25 16:11

Vittorio Romeo