C++ &引用;没有匹配的函数调用“;在C+中执行多线程时出错+;
我不明白我做错了什么。我已经阅读了几个例子以及CPP参考资料,但什么也没想到 当我尝试使用for循环执行多个线程时,我调用一个函数“evaluate”。当我以串行方式运行程序时,没有编译问题,但是添加多线程会产生以下结果:C++ &引用;没有匹配的函数调用“;在C+中执行多线程时出错+;,c++,multithreading,C++,Multithreading,我不明白我做错了什么。我已经阅读了几个例子以及CPP参考资料,但什么也没想到 当我尝试使用for循环执行多个线程时,我调用一个函数“evaluate”。当我以串行方式运行程序时,没有编译问题,但是添加多线程会产生以下结果: GraphEvaluate.cpp:35:70: error: no matching function for call to ‘std::thread::thread(<unresolved overloaded function type>, const s
GraphEvaluate.cpp:35:70: error: no matching function for call to ‘std::thread::thread(<unresolved overloaded function type>, const std::vector<std::vector<double> >&, const std::vector<InVec>&, InVec&, Graph&)’
t[iii] = std::thread(evaluate, controlSpace, stateSpace, box, graph);
GraphEvaluate.cpp:35:70:错误:调用“std::thread::thread(,const std::vector&,const std::vector&,InVec&,Graph&)”时没有匹配的函数
t[iii]=std::thread(evaluate、controlSpace、stateSpace、box、graph);
我不明白“evaluate”怎么会是“未解析的重载函数类型”
代码如下:
...
std::thread t[g_threads-1];
int counter(0);
for(int iii = 0 ; iii < (g_threads - 1) ; ++iii)
{
InVec box(stateSpace.at(counter));
t[iii] = std::thread(evaluate, controlSpace, stateSpace, box, graph);
counter += 1;
}
for(int iii = 0 ; iii < (g_threads - 1) ; ++iii)
{
t[iii].join();
}
...
。。。
标准:螺纹t[g_螺纹-1];
整数计数器(0);
对于(int iii=0;iii<(g_螺纹-1);+iii)
{
InVec箱(状态空间位于(柜台));
t[iii]=std::thread(evaluate、controlSpace、stateSpace、box、graph);
计数器+=1;
}
对于(int iii=0;iii<(g_螺纹-1);+iii)
{
t[iii].join();
}
...
以及评估功能:
void evaluate(const std::vector<std::vector<double>> &controlSpace,
const std::vector<InVec> &stateSpace, InVec box, Graph &graph)
{
std::vector<InVec> boxList; // create empty vector of InVec objects
SPnode ASP(box); // create subpaving node with box
mince(ASP, g_epsilon); // mince box
treeToList(ASP, boxList); // convert tree to list for efficient mapping
// map each box in boxList with mapping defined in GraphMapping.cpp for each
// controller value
for (auto control : controlSpace)
{
ImList imageList;
for (auto box : boxList)
{
imageList.addBox(mapping(box, control));
}
InVec intersectionBox(inclusionMap(imageList));
std::vector<InVec> intersectingBoxes; // list of boxes in state space
// that intersect with intersectionBox
for (auto ssBox : stateSpace)
{
if (!(noIntersection(ssBox, intersectionBox)))
intersectingBoxes.push_back(ssBox);
}
std::vector<int> nodeList; // list of nodes that box (function input)
// points to with the given control
if (!(intersectingBoxes.empty()))
{
for (auto ssBox : intersectingBoxes)
{
for (auto image : imageList.getList())
{
if (!(noIntersection(ssBox, image)))
{
nodeList.push_back(ssBox.getBoxNumber());
break;
}
}
}
}
if (!(nodeList.empty()))
{
for (auto node : nodeList)
{
graph.setAdjList(box.getBoxNumber(), Edge(node, control));
}
}
}
}
void求值(const std::vector&controlSpace,
常量标准::向量和状态空间、InVec框、图和图)
{
std::vector boxList;//创建InVec对象的空向量
SPnode ASP(box);//使用box创建子保存节点
切碎(ASP,g_epsilon);//切碎盒
树列表(ASP,boxList);//将树转换为列表以实现高效映射
//使用GraphMapping.cpp中为每个框定义的映射映射boxList中的每个框
//控制器值
用于(自动控制:controlSpace)
{
ImList图像列表;
用于(自动框:框列表)
{
addBox(映射(框,控件));
}
InVec交叉框(包括地图(图像列表));
std::vector IntersectingBox;//状态空间中的框列表
//与intersectionBox相交的
用于(自动ssBox:stateSpace)
{
如果(!(无交叉(ssBox,交叉框)))
交叉框。推回(ssBox);
}
std::vector nodeList;//框中的节点列表(函数输入)
//指向具有给定控件的
if(!(intersectingbox.empty())
{
用于(自动ssBox:相交框)
{
对于(自动映像:imageList.getList())
{
if(!(noIntersection(ssBox,image)))
{
nodeList.push_back(ssBox.getBoxNumber());
打破
}
}
}
}
if(!(nodeList.empty())
{
用于(自动节点:节点列表)
{
graph.setAdjList(box.getBoxNumber(),边(节点,控件));
}
}
}
}
非常感谢您的帮助。std::thread的构造函数推断其参数类型并按值存储它们 C++模板函数参数类型推断机制从类型为
T&
的参数推断类型T
。因此,std::thread
的所有参数都是按值传递的@马克西梅戈拉什金
如果需要调用其参数是来自std::thread
的引用的函数,请使用std::ref()
包装该参数。例如:
std::thread(evaluate, std::ref(controlSpace), std::ref(stateSpace), box, std::ref(graph));
另一方面,您需要更改evaluate
函数的声明,如下所示:
void evaluate(std::vector<std::vector<double>> controlSpace,
std::vector<InVec> stateSpace, InVec box, Graph graph);
void求值(标准::向量控制空间,
std::向量状态空间、InVec框、图);
有关更多信息,请参阅。您需要使用
std::ref
,即std::thread(evaluate,std::ref(controlSpace),std::ref(stateSpace),box,std::ref(graph))
奇怪的是,这似乎不起作用,我得到了与上面相同的错误消息,只是在包装参数前面有std::reference_wrapperevaluate
一个成员类?结果是evaluate
是我在另一个代码中使用的函数,但作为我的一些对象的朋友包含了它。我仍然觉得奇怪,std::thread
会在我的其他函数都找不到的情况下找到这个好友声明。有什么想法吗?