2017-05-31 40 views
0

大規模なCSVファイルに対して複数のプロセスを一度に実行するためにasync関数を使用して、長い待ち時間を避けようとしましたが、エラーが発生します:オーバーロード機能のインスタンスがありません "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)); 

は、あなたはそれがないと思う何をしません。すぐに呼び出され、 voidを返す countTotal(lcount)が評価されます。したがって、コードは無効です。


documentation for std::asyncをご覧ください。それにはCallableオブジェクトが必要です。 countTotalためCallableを生成して実行を延期する最も簡単な方法は、ラムダを使用している:

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

"100"はlcount変数を使用しても機能しますか? –

+0

はい、ラムダ本体の中の 'int&'で 'countTotal'を呼び出すことができます。私は自分の答えを固定しました。 –

+0

または 'auto r = async(launch :: async、countTotal、std :: ref(lcount));' – WhiZTiM

関連する問題