【问题标题】:C++ threading - no matching function for call toC++ 线程 - 没有匹配的调用函数
【发布时间】:2018-02-26 13:40:51
【问题描述】:

这是我第一次在 C++ 中使用线程,我遇到了一些问题。我收到错误

error: no matching function for call to ‘std::thread::thread(<unresolved overloaded function type>, __gnu_cxx::__alloc_traits<std::allocator<packetInfo> >::value_type*)’

这是我的代码摘录:

std::vector<packetInfo> sentPackets; // global var

void renewIP(struct packetInfo *currentPacket) {
...//code
}

void anotherFuntion() {

    ...
    std::thread renewTimer(renewIP, &(sentPackets[i]));
    renewTimer.detach();
    ...
}

我完全不知道我做错了什么或为什么会出错。

谢谢。

【问题讨论】:

  • 我没有尝试编译这段代码,但它显然比问题描述的问题多得多。贴出真实代码。
  • 我尝试取消对函数名的引用,但我得到了同样的错误。我将它编译为 -std=c++11

标签: c++ multithreading syntax-error


【解决方案1】:

错误直接告诉你:

<unresolved overloaded function type>

你必须有多个renewIP 的重载,编译器不知道你想要哪一个。您可以重命名它们以使它们不模棱两可,或者通过强制转换使其明确:

std::thread renewTimer((void(*)(struct packetInfo*))renewIP, &(sentPackets[i]));

【讨论】:

  • 你说得对。我更改了函数参数并忘记在头文件中也进行更改。抱歉这个愚蠢的问题。
猜你喜欢
  • 1970-01-01
  • 2021-09-21
  • 2016-11-23
  • 2012-05-27
  • 1970-01-01
  • 1970-01-01
  • 2017-05-08
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多