C++中多线程处理时出现"no matching function call"错误

"no matching function call" error when multithreading in C++

本文关键字:no matching call 错误 function 多线程处理 C++      更新时间:2023-10-16

我不明白我做错了什么。我已经遍历了几个示例以及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);

>

相关文章: