C++中多线程处理时出现"no matching function call"错误
"no matching function call" error when multithreading in C++
我不明白我做错了什么。我已经遍历了几个示例以及cppreference,但什么也没找到。
当我尝试使用for循环执行多个线程时,我调用函数"evaluate"。当我连续运行程序时,没有编译问题,但是添加多线程会产生以下结果:
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);
我不明白为什么"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();
}
...
和evaluate函数:
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));
}
}
}
}
std::thread
的构造函数推导其参数类型并按值存储。
c++模板函数参数类型推导机制从
T&
类型的参数中推导出T
类型。因此,std::thread
的所有参数都是按值传递的。@MaximEgorushkin
如果你需要调用一个函数,它的参数是来自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);
>
相关文章:
- "error: no matching function for call to"构造函数错误
- 调用专用模板时出错"no matching function for call to [...]"
- "no matching function for call to 'Vector::Vector'"错误
- 我在 ifstream input_file(文件名)行中收到错误"no matching function to call";
- 为什么当我在 std::string 中打开文件名的 fstream 时出现"no matching function"错误?
- 从 TVector2 类派生复杂类:"No matching function for call to Complex::Complex()"
- 学习 c++,编译错误"no matching function for call"
- C++ "error: no matching function"
- 尝试实例化模板类的对象时出现"No Matching Constructor"错误
- 结构错误:"no matching function for call to "
- 创建函数变体向量时"No matching function for call"错误
- 为什么"std::isdigit"在 Windows 上报告C++"no matching overloaded function"
- 编译时二叉搜索树错误的反向迭代器表示"no matching function call for operator=()"
- 如果子类中没有构造函数方法,则错误"no matching function for call to 'LGame::LGame(String&)'"
- 提升多精度浮点数128:标准::exp错误:'no matching function for call'
- "error: no matching function for call to",我无法更改结构的定义
- 替换派生对象向量中的对象"no matching function to call"
- 我的程序显示许多错误,"error: no matching function for call to 'getPercentScore'"
- 为什么即使我定义了它们"no matching overload found"也会出现错误
- 为什么有一个指向此代码行(第 7 行)的"no matching call for function"错误?