This question already has answers here:
Passing arguments to std::async by reference fails
                                
                                    (3个答案)
                                
                        
                                7个月前关闭。
            
                    
我试图将函数countVocals()(返回一个整数)传递给future的向量,但出现此问题:

'No matching function for call to async:'
candidate template ignored


我该如何解决?

我已经尝试使用以下语法和lambda表达式进行尝试,但是我遇到了相同的问题

int countVocals(std::string const &fileName, int &found);
std::vector<int> vocals(argc-1, 0);
std::vector<std::future<int>> futures(argc-1);

for (int i = 1; i < argc; i++) {
    futures.push_back(std::async(countVocals, std::string(argv[i]), vocals.at(i-1)));

最佳答案

countVocals需要引用,因此您需要将引用显式传递给st::async

futures.push_back(std::async(countVocals, std::string("Bla"), std::ref(vocals.at(i))));
                                                              ^^^^^^^^

08-08 01:22