首页 > 解决方案 > 为什么它说#include包含在c++中时不包含?

问题描述

我是多线程的新手,但我在这个网站上找到了一个关于简单多线程代码的答案。我完全复制了它,#include <thread>但后来它在终端(我正在运行 Visual Studio Code)中说我没有包含线程。这是代码。

#include <string>
#include <iostream>
#include <thread>

using namespace std;

void task1(string msg)
{
    cout << "task1 says: " << msg;
}

int main()
{

    thread t1(task1, "Hello");

    t1.join();
}

这是我看到的错误:

c:\Users\Lol\Desktop\C++\Thread testing\Threadtest.cpp: In function 'int main()':
c:\Users\Lol\Desktop\C++\Thread testing\Threadtest.cpp:18:5: error: 'thread' was not declared in this scope
   18 |     thread t1(task1, "Hello");
      |     ^~~~~~
c:\Users\Lol\Desktop\C++\Thread testing\Threadtest.cpp:5:1: note: 'std::thread' is defined in header '<thread>'; did you forget to '#include <thread>'?
    4 | #include <thread>
  +++ |+#include <thread>
    5 |
c:\Users\Lol\Desktop\C++\Thread testing\Threadtest.cpp:24:5: error: 't1' was not declared in this scope; did you mean 'tm'?
   24 |     t1.join();
      |     ^~
      |     tm
The terminal process terminated with exit code: 1

标签: c++multithreadingerror-correction

解决方案


推荐阅读