eclipse 功能睡眠无法解决

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7893666/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 17:14:09  来源:igfitidea点击:

function sleep could not be resolved

c++eclipsesleep

提问by andrea

I'm using eclipse and I'm building a simple program, but I get an error saying function sleep could not be resolved

我正在使用 eclipse 并且我正在构建一个简单的程序,但是我收到一条错误消息,说函数 sleep 无法解析

#include <time.h>
#include <stdio.h>
#include <conio.h>
#include <iostream>
using namespace std;

int main() {
    cout << "!!!Hello World!!!" << endl; // prints !!!Hello World!!!
    printf("ciao");
    sleep(20);
    return 0;
}

I don't know if I need other libraries or something else. MinGW should be installed properly, so I have no idea

我不知道我是否需要其他图书馆或其他东西。MinGW应该安装正确,所以我不知道

回答by Keith Thompson

The sleep()function is defined by POSIX, not by the C++ standard.

sleep()函数由 POSIX 定义,而不是由 C++ 标准定义。

If you're on a Unix-like system, you need

如果您使用的是类 Unix 系统,则需要

#include <unistd.h>

If you're not, then the sleep()function might not even be available.

如果不是,则该sleep()功能甚至可能不可用。

Oh, and mixing cout << ...and printf()is probably not a good idea, and you don't need the #include <conio.h>.

哦,混合cout << ...printf()可能不是一个好主意,而且您不需要#include <conio.h>.

回答by Mark Wilkins

If you are using MinGW as stated, then you may need to include windows.h. The sleep implementation I think uses the Win API Sleep().

如果您按照说明使用 MinGW,则可能需要包含windows.h. 我认为 sleep 实现使用 Win API Sleep()

For example:

例如:

#include <windows.h>
#include <iostream>

int main() {
    std::cout << "!!!Hello World!!!" << std::endl;
    Sleep(20000);
    std::cout << "Text Will Appear After 2 Sec.." << std::endl;
    return 0;
}

回答by Lou

When developing portable code that must run on unix and windows, I've always defined a sleep() macro for windows that calls the windows Sleep() that looks like this:

在开发必须在 unix 和 windows 上运行的可移植代码时,我总是为 windows 定义一个 sleep() 宏,它调用 windows Sleep() ,如下所示:

#define sleep(a) Sleep(a * 1000)

It's simple enough to do.

做起来很简单。