简体   繁体   English

没有匹配的 function 可以打电话给<unresolved overloaded function type></unresolved>

[英]no matching function for call to <unresolved overloaded function type>

I can't relate with similar questions.我无法回答类似的问题。 That's my MRE, basically I'd like to overload fun with a version accepting a template reference.那是我的 MRE,基本上我想用一个接受模板引用的版本来重载fun It all works until std::thread enters in the game.这一切都有效,直到std::thread进入游戏。 It seems I'm missing something from its constructor.看来我从它的构造函数中遗漏了一些东西。

Error shown on g++-10 is g++-10 上显示的错误是

error: no matching function for call to ‘std::thread::thread(<unresolved overloaded function type>, std::string, std::shared_ptr<MySem>)’
   43 |   std::make_shared<MySem>());
#include <string>
#include <memory>
#include <thread>

class MyData
{};

class MySem
{};

template <typename T, typename Sem>
void fun(T & t, const std::string & TAG, std::shared_ptr<Sem> upSem)
{}

template <typename T, typename Sem>
void fun(const std::string & TAG, std::shared_ptr<Sem> upSem)
{
    T t;
    fun(t, TAG, upSem);  // NO std::ref(t)
}

int main(int argc, char ** argv)
{
    MyData d;

    fun<MyData, MySem>(
        "works",
        std::make_shared<MySem>());

    fun<MyData, MySem>(
        d,
        "this too",
        std::make_shared<MySem>());


    std::thread t1(fun<MyData, MySem>,
        std::string("this doesn't"),
        std::make_shared<MySem>());               // line 43

    std::thread t2(fun<MyData, MySem>,
        d,
        std::string("this neither"),
        std::make_shared<MySem>());

    return 0;
}

If you have an overloaded function and want to pick a specific overload, you have to cast it manually to get the right one like:如果您有一个重载的 function 并且想要选择一个特定的重载,您必须手动转换它以获得正确的重载,例如:

std::thread t1(static_cast<void(*)(const std::string&, std::shared_ptr<MySem>)>(&fun<MyData, MySem>),
        std::string("this doesn't"),
        std::make_shared<MySem>());               // line 43

As already given in the comment from "Passer By", using a lambda simplifies the whole thing a lot and makes the code more readable.正如“路人”的评论中已经给出的那样,使用 lambda 可以大大简化整个过程并使代码更具可读性。

See also here: other answer另请参阅此处: 其他答案

My guess is that the constructor of std::thread cannot resolve which overload of fun you're trying to call.我的猜测是std::thread的构造函数无法解析您要调用的fun重载。 No idea why though.不知道为什么。

Having only one version of fun such as只有一种fun ,例如

template <typename T, typename sem>
void fun(const std::string&, std::shared_ptr<sem>)
{
    ...
}

Allows you to construct t1 fine (but t2 will obviously fail).允许您构建t1好(但t2显然会失败)。

A workaround is to pass a lambda instead, such as:解决方法是改为传递 lambda,例如:

std::thread t3([&](){fun<data, sem>(d, "works again", std::make_shared<sem>());});
std::thread t4([&](){fun<data, sem>("this too", std::make_shared<sem>());});

The problem is not related with the std::thread function, the problem is that the compiler does not know which overloaded function you are requesting, using a lambda as a workaround could do the job:问题与std::thread function 无关,问题是编译器不知道您请求的是哪个重载 function,使用 lambda 作为变通方法可以完成这项工作:

    std::thread t1([=]{ 
        fun<MyData, MySem>(std::string("This doesn't"),  std::make_shared<MySem>()); 
        } );
    t1.join();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 没有匹配的函数可调用未解析的重载函数类型 - No matching function for call to unresolved overloaded function type 没有匹配的函数无法调用未解析的重载函数类型 - no matching function for call unresolved overloaded function type “没有匹配的函数可以调用...未解析的重载函数类型” - “No matching function for call to… unresolved overloaded function type” 没有用于调用std :: transform,未解析的重载函数类型的匹配函数 - No matching function for call to std::transform, unresolved overloaded function type 错误:没有匹配的函数可以调用&#39;sort(...,…, <unresolved overloaded function type> )&#39; - error: no matching function for call to 'sort(…, …, <unresolved overloaded function type>)' 没有匹配的函数来调用&#39;QDomDocument :: createElement( <unresolved overloaded function type> )&#39; - no matching function for call to 'QDomDocument::createElement(<unresolved overloaded function type>)' 重载的电话*** <unresolved overloaded function type> )&#39;不明确 - call of overloaded *** <unresolved overloaded function type>)' is ambiguous 没有匹配的函数来调用&#39;std :: list <int, std::allocator<int> &gt; :: sort( <unresolved overloaded function type> )&#39; - No matching function for call to 'std::list<int, std::allocator<int> >::sort(<unresolved overloaded function type>)' “没有匹配函数来调用'async(std :: launch,<unresolved overloaded function type>,std :: string&)'” - “no matching function for call to ‘async(std::launch, <unresolved overloaded function type>, std::string&)’” 错误:没有匹配的函数调用 &#39;sf::RenderWindow::draw(<unresolved overloaded function type> )&#39; SFML C++ - Error: no matching function for call to 'sf::RenderWindow::draw(<unresolved overloaded function type>)' SFML C++
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM