为什么找不到Sleep()函数?

时间:2015-12-13 06:24:27

标签: c++

提前致谢;我是C ++的新手。

我在Windows 10上使用Visual Studio Express for Desktop 15.我正在制作一个控制台程序。

我已经包含了所有这些标题:

#include "stdafx.h"
#include <iostream>
#include <string>
#include "New.h"
#include <time.h>
//#include <windows.h> Commented out because it causes a lot of extra errors
#include <dos.h>
#include <stdio.h>
#include <conio.h>

我使用像这样的命令

Sleep(4)

我收到错误,找不到睡眠标识符。为什么呢?

在查看之后,只有一行(多次出现)给我错误。

cin.ignore(numeric_limits<streamsize>::max(), '\n');

语法错误:

'(' Illegal token on right side

预期的标识符

修改

我认为dos.h是当时需要的睡眠标题,而不是windows.h

包含windows.h的问题在于它产生了错误!每个人都没有解决这个问题,而是告诉我只是对它进行评论。我不能这样做,这就是重点

1 个答案:

答案 0 :(得分:3)

正如@Jonathan_Potter所说,SleepWindows API&amp;的一部分。因此,您需要包含windows.h,因此我不认为您需要进一步了解您的代码无法正常工作的原因。但是我写这个答案给你一个标准化的替代方案来解决你的睡眠问题&#34;。

C ++ 11引入了sleep_for&amp; sleep_until在命名空间std::this_thread中定义。当前者持续一段时间时,后者是绝对时间。我举一个sleep_for的例子: -

#include <iostream>
#include <thread>
#include <chrono>
using namespace std;
int main()
{
    cout << "start" << endl;
    this_thread::sleep_for (chrono::seconds(1));  // for C++14 users you can simply write 1s to denote 1 second
    cout << "end\n";   // will be printed after 1 second of printing 'start'
    return 0;
}

我给你这个答案,因为你应该总是喜欢使用标准中的功能,以使你的程序可移植。只有在真正需要的情况下,您才可以与此建议不同。