2017-05-31 49 views
0

我想用异步函数在一个大的csv文件上一次运行多个进程,以避免长时间等待用户但是我得到的错误:没有实例的重载函数“async”匹配参数列表

no instance of overloaded function "async" matches the argument list 

我已经搜索了一下,没有发现任何修复它,并出于想法,因为我很新的编码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; 
} 
+2

'lcount'是一个全局变量,也由各地参照通过所有功能通过。在这一点上,它不需要是全球性的。此外,您正在为全局变量和对该变量的引用使用相同的名称。它会清除那个令人困惑的位。 –

回答

0

线

auto r = async(launch::async, countTotal(lcount)); 

没有做什么,你认为它。它立即调用并评估countTotal(lcount),它返回void。该代码因此无效。


看看documentation for std::async。它需要一个Callable对象。最简单的方法来产生一个countTotalCallable推迟执行使用Lambda:

auto r = async(launch::async, []{ countTotal(lcount); }); 
+0

如果我使用变量lcount,那么“100”还能工作吗? –

+0

是的,你可以用lambda体内的任何'int&'调用'countTotal'。 '100'实际上不会工作,因为它是一个右值 - 我确定了我的答案。 –

+0

或'auto r = async(launch :: async,countTotal,std :: ref(lcount));' – WhiZTiM

相关问题