我正在尝试使用异步函数一次在大型csv文件上运行多个进程,以避免用户等待很长时间但是我收到错误:
no instance of overloaded function "async" matches the argument list
我已经google了,没有找到任何修复它的东西,而且我没有想法,因为我对C ++编码很新,任何帮助都会非常感激!我在下面列出了所有代码。
#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;
}
答案 0 :(得分:0)
该行
auto r = async(launch::async, countTotal(lcount));
不符合您的想法。它立即调用并评估countTotal(lcount)
,返回void
。因此代码无效。
查看documentation for std::async
。它需要Callable
个对象。为Callable
生成countTotal
并推迟执行的最简单方法是使用lambda:
auto r = async(launch::async, []{ countTotal(lcount); });