尝试构造std :: thread时出现奇怪的编译器错误:

Vis*_*ani 0 c++ c++11 stdthread

以下程序无法编译 g++ -std=c++11 -Wall -Werror test.cpp -o test.o:

#include <thread>
using namespace std;

void fill(int n) {
    return;
}

int main() {
    thread test(fill, 5);
}
Run Code Online (Sandbox Code Playgroud)
test.cpp:9:12: error: no matching constructor for initialization of 'std::__1::thread'
    thread test(fill, 5);
           ^    ~~~~~~~
Run Code Online (Sandbox Code Playgroud)

难道是因为fill与发生冲突std::fill#include <algorithm>?我没有把它包括在内,但我想<thread>可能会这样.

将我的函数名称更改为fillie(或其他任何东西)允许它在没有链接的情况下正确编译pthread.

我问,因为它是一个奇怪的编译器错误消息,并且它也意味着线程构造函数不能消除我根据参数使用哪个函数(哪种有意义,但想要确认).

Joh*_*nck 5

是的,问题是因为不知道是否fillstd::fill你的全局fill功能.

解决它的一种方法是写入::fill以明确使用全局的.

  • 或者做好事而不要调用`using namespace std;` (2认同)