Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/147.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 异步传递时如何在函数中传递多个参数_C++_Asynchronous_Parameter Passing - Fatal编程技术网

C++ 异步传递时如何在函数中传递多个参数

C++ 异步传递时如何在函数中传递多个参数,c++,asynchronous,parameter-passing,C++,Asynchronous,Parameter Passing,我想为一个函数传递两个参数,当该函数在异步函数中传递时,该函数将两个参数作为参数。我以前从未使用过async,所以我不知道如何做到这一点 这就是函数 double NearestPoints::otherCoordinate(Coordinate coordinate1, Coordinate** secondCoordinate){ 这是异步函数 std::future<double> ret = std::async(&otherCoordinate,coordinat

我想为一个函数传递两个参数,当该函数在异步函数中传递时,该函数将两个参数作为参数。我以前从未使用过async,所以我不知道如何做到这一点

这就是函数

double NearestPoints::otherCoordinate(Coordinate coordinate1, Coordinate** secondCoordinate){
这是异步函数

std::future<double> ret = std::async(&otherCoordinate,coordinate1,ref(coordinate2));
std::future ret=std::async(&otherCoordinate,coordinate1,ref(coordinate2));
我很确定我以错误的方式实现了这个函数,但我只想知道正确的实现方式


提前谢谢

这就是你要找的吗

#include <iostream>    
#include <future>      


int add(int x,int y) {    
    return x+y;
}

int main()
{  
    std::future<int> fut = std::async(add, 10,20);
    int ret = fut.get();
    std::cout << ret << std::endl;       
    return 0;
}
#包括
#包括
整数加(整数x,整数y){
返回x+y;
}
int main()
{  
std::future fut=std::async(add,10,20);
int-ret=fut.get();

std::cout从您的问题中我可以看出,您似乎忘记了将
NearestPoints
实例传递给
std::async
调用。由于
NearestPoints::otherCoordinate
是一个成员函数,它需要为其
指针传递的
NearestPoints
类的实例

要解决此问题,应传入当前实例的副本,以便函数可以访问要操作的实例

您对
std::async
的固定调用如下所示:

std::future<double> ret = std::async(&NearestPoints::otherCoordinate, *this, coordinate1, std::ref(coordinate2));
std::future ret=std::async(&NearestPoints::otherCoordinate,*this,coordinate1,std::ref(coordinate2));

按如下方式修复您的呼叫:

std::future<double> ret = std::async(&NearestPoints::otherCoordinate,&instance_name,coordinate1,std::ref(coordinate2));
std::future ret=std::async(&NearestPoints::otherCoordinate,&instance_name,coordinae1,std::ref(coordinate2));

请注意,
otherCoordinate
是一个成员函数。还需要传入
NearestPoints
的实例。我不是在main中调用async。我是在另一个函数的for循环中调用它,当我尝试给我这个错误时,该函数也是NearestPoints的成员函数:“&”:非法操作on绑定成员函数表达式My bad,您需要将类名放在成员函数名之前,即使您的函数在类范围内。已修复。现在它会出现以下两个错误。错误1:无法专门化函数模板的未知类型std::invoke(_Callable&,_Types&&…)“NearestPoints.Error 2:“initializing”:无法从“std::future”转换为“std::future”NearestPoints我不完全确定返回类型不明确的原因。要解决明显的不明确问题,我所做的是显式指定函数指针的类型以解决任何可能的不明确问题。请参阅我更新的答案。仍然给出相同的错误。还有一件事。我正在调用另一个成员函数中的async,该函数不是在main中的NearestPoints