【发布时间】:2020-12-14 08:11:40
【问题描述】:
我把它写成一个多线程示例的简化版本来感受一下,但是在编译时遇到了一些问题。我的编译器说thread 不是std 的成员,并提示我将#include <thread> 添加到我的代码中,即使我已经这样做了。到目前为止,我一直无法找到任何类似的问题,但我怀疑这是我如何编译它的问题,因为我的代码与示例代码非常相似。
#include <iostream>
#include <thread>
void doWork () {
std::cout << "Working...\n";
}
int main () {
std::thread worker(doWork);
work.join();
std::cout << "Finished\n";
return 0;
}
我的编译器是 MinGW g++ 9.2.0
我用g++ main.cpp -o main 编译它给了我这些错误:
main.cpp: In function 'int main()':
main.cpp:9:7: error: 'thread' is not a member of 'std'
9 | std::thread worker(doWork);
| ^~~~~~
main.cpp:3:1: note: 'std::thread' is defined in header '<thread>'; did you forget to '#include <thread>'?
2 | #include <thread>
+++ |+#include <thread>
3 |
main.cpp:11:2: error: 'work' was not declared in this scope
11 | work.join();
| ^~~~
【问题讨论】:
-
需要在命令行中添加
-std=c++11。 -
我们至少可以为您修复最后一个错误:
work应该是worker。 -
@user4581301 哦,是的,我错过了。
标签: c++ multithreading mingw