首先,我想创建一个程序,它将从用户输入时间计算到零的时间,这是代码 -
#include <iostream>
#include <stdio.h>
#include <time.h>
#include <unistd.h>
#include <dos.h>
#include <windows.h>
#include <thread>
using namespace std;
// sleep(5000);
int runTimer = 1;
int seconds;
int hoursLeft;
int minutesLeft;
int secondsCount=0;
void timeLeft ()
{
hoursLeft = seconds/3600;
minutesLeft = seconds/60 - hoursLeft*60;
}
void timer ()
{
while(runTimer=1)
{
if (secondsCount == 60)
{
timeLeft();
cout << "The Amount of time left is: " << hoursLeft << " hours and " << minutesLeft << " minutes left." << endl;
secondsCount=0;
}
secondsCount++;
seconds--;
Sleep(1000);
timer();
}
}
int main()
{
// introduction and time picking
cout << "Welcome to my Timer - Please set the amount of hours and than minutes you want the timer to run" << endl;
double requestedHours, requestedMinutes;
cin >> requestedHours;
cin >> requestedMinutes;
double requestedSeconds = requestedHours*3600 + requestedMinutes*60;
seconds = requestedSeconds;
cout << "Timer Started";
timer();
}
但我想添加一个功能,用户可以在其中键入一个单词或一个字母来暂停程序,(经过一番研究后我发现了线程) - 但是当我添加#include <thread>
我得到了这个按摩 -
"#error This file requires compiler and library support for the \
ISO C++ 2011 standard. This support is currently experimental, and must be \
enabled with the -std=c++11 or -std=gnu++11 compiler options.
#endif"
如何解决这个问题?
您似乎在 Windows 中使用 g++,所以我认为它是 Code::Blocks 附带的 MinGW 风格。
GNU glibc 不支持 Windows 线程(其开发团队不关心 Windows),因此您必须使用 MinGW pthread 构建,或使用附加组件。
首先你应该添加-std=c++11
到您的构建选项。
但是,您的错误消息表明您安装了相当旧版本的 g++,因此我建议升级到 Mingw-w64(Mingw 的一个积极维护的分支)。See here https://stackoverflow.com/questions/29947302/meaning-of-options-in-mingw-w64-installer以获得安装帮助。
有关 MinGW 各个版本中线程支持的更多信息,请参阅这个线程 https://stackoverflow.com/questions/15718904/mingw-and-stdthread。我成功地将 Mingw-w64 与 Win32 线程和 Code::Blocks 中的 meganz 插件一起使用。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)