【问题标题】:thread is not a member of std c++线程不是 std c++ 的成员
【发布时间】:2019-01-31 01:45:49
【问题描述】:

我遇到了无法使用线程的问题,因为我不断收到错误消息“'thread' is not a member of 'std'”。

我正在使用带有 c++11 的 MinGW 作为编译器标志。在我昨天制作的小程序上,它运行良好。基本上我想在玩伪随机数的小猜谜游戏时播放“哔”的歌曲。

int rnumber, guess, maxrand;

std::thread t1(pinkpanther);
t1.detach();

cout << "What do you want the maximum Number to be? ";
cin >> maxrand;

rnumber = randy(maxrand);

//Start
cout << endl << "This is a game!" << endl << "You have 5 tries to guess the random number generated by this program, have fun" << endl;

for (int i = 0; i < 5; i = i + 1)
{
    cout << "Your guess: ";
    cin >> guess;

    if (guess < rnumber)
    {
        cout << "Your guessed number is smaller than the answer! Try again!" << endl << endl;
    }
    else if (guess > rnumber)
    {
        cout << "Your guessed number is bigger than the answer! Try again!" << endl << endl;
    }
    else
    {
        cout << "you guessed the right number!";
        break;
    }
}





return 0;

它总是给我同样的错误 ||In function 'int main()':| 'thread' is not a member of 'std'| 't1' was not declared in this scope| ||=== Build failed: 2 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===| 我真的不知道为什么了

编辑:pinkpanther() 只播放我在“哔”中找到的粉红豹歌曲

Edit2:我有几个库(windows、thread、ctime 和 ctdlib)

【问题讨论】:

  • #include &lt;thread&gt;了吗?
  • 似乎前几行在复制和粘贴过程中丢失了。有些行不太相关,但很可能第一行(或缺少某些东西的事实)很重要
  • 是的,我有,我会编辑让你看到我
  • 另外请阅读minimal reproducible example。如果您的代码不包含与问题无关的内容,我们更容易为您提供帮助。例如,我敢打赌,如果你删除了整个循环,你的代码会产生同样的错误
  • MinGW 以不支持std::thread 而闻名。 MinGW-w64 是一个替代方案。

标签: c++ multithreading random std


【解决方案1】:

你提供的代码永远不会#include &lt;thread&gt;。那么编译器应该如何知道std::thread 是什么?

通过将#include &lt;thread&gt; 添加到文件顶部来解决您的问题。

还要确保使用真正支持 C++11(或更高版本)和 std::thread 的编译器。

【讨论】:

  • 对不起,我是新来的,当然应该写下我包含的库,但它被包含了
  • @SergeyA MinGW-w64 确实如此,并且自 GCC 4.7 以来一直如此。
  • 所以我会再次尝试使用 MinGW-w64 并报告它是否有帮助
【解决方案2】:

您使用的是什么 MinGW 版本?如果你有一个 Win32 线程模型,它不支持 C++11 线程;您应该使用 POSIX 线程模型(基于 winpthreads)来获取一个,但请注意,它有很多错误。

【讨论】:

  • 越野车是什么意思?
  • 我下载了最新的 mingw(我认为是 6.3.0-1?),我还有一个程序正在运行,它只是编译没有问题
  • @rubenvb 我的意思是我们失去了一周的时间来跟踪线程启动中的竞争条件到 winpthreads 代码,我们设法将错误减少到一个简单的可重现情况并且错误仍然存​​在。在看到 winpthreads 的内部结构后,我不想用十英尺长的杆子碰那些东西。目前,我们正在使用带有 Win32 线程模型的 MinGW 构建(顺便说一句,由您制作!)和 libstdc++ 标头,并使用 std::thread & co 的 vanilla C++/Win32 重新实现进行修补。
  • @Grimmboy gcc+MinGW 发行版以几种不同的“风格”存在,特别是关于线程模型(Win32、POSIX 和新的实验模型)和异常处理模型(sjlj、dw2 和 SEH),和二进制版本由不同的人/项目提供。说您拥有“最新版本”并没有多大意义。
  • 最新的常用 MinGW 是 8.1.0 - 从 nuwen.net 获取 - 它支持 std::thread。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2010-11-29
  • 1970-01-01
  • 2019-07-04
  • 1970-01-01
  • 1970-01-01
  • 2019-03-29
  • 1970-01-01
相关资源
最近更新 更多