Linux C++ 错误:未在此范围内声明睡眠
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10976176/
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
C++ error : Sleep was not declared in this scope
提问by Mahika
I am using C++ in Ubuntu with codeBlocks, boost 1.46 in GCC 4.7 [ yield_k.hpp ]
我在 Ubuntu 中使用 C++ 和 codeBlocks,在 GCC 4.7 中提升 1.46 [yield_k.hpp]
I get this compile time error:
我收到此编译时错误:
error : Sleep was not declared in this scope
Code:
代码:
#include <iostream>
using namespace std;
int main() {
cout << "nitrate";
cout << flush;
sleep(1000);
cout << "firtilizers";
return 0;
}
How do I resolve this error? I want the program to hang for 1 second.
如何解决此错误?我希望程序挂起 1 秒钟。
采纳答案by moshbear
Sleep
is a Windows function.
Sleep
是一个 Windows 函数。
For Unix, look into using nanosleep
(POSIX) or usleep
(BSD; deprecated).
对于 Unix,请考虑使用nanosleep
(POSIX) 或usleep
(BSD;已弃用)。
A nanosleep
example:
一个nanosleep
例子:
void my_sleep(unsigned msec) {
struct timespec req, rem;
int err;
req.tv_sec = msec / 1000;
req.tv_nsec = (msec % 1000) * 1000000;
while ((req.tv_sec != 0) || (req.tv_nsec != 0)) {
if (nanosleep(&req, &rem) == 0)
break;
err = errno;
// Interrupted; continue
if (err == EINTR) {
req.tv_sec = rem.tv_sec;
req.tv_nsec = rem.tv_nsec;
}
// Unhandleable error (EFAULT (bad pointer), EINVAL (bad timeval in tv_nsec), or ENOSYS (function not supported))
break;
}
}
You will need <time.h>
and <errno.h>
, available in C++ as <ctime>
and <cerrno>
.
您将需要<time.h>
and <errno.h>
,可在 C++ 中作为<ctime>
and 使用<cerrno>
。
usleep
is simpler to use (just multiply by 1000, so make it an inline function). However, it's impossible to guarantee that that sleeping will occur for a given amount of time, it's deprecated, and you need to extern "C" { }
-include <unistd.h>
.
usleep
使用起来更简单(只需乘以 1000,因此将其设为内联函数)。但是,无法保证在给定的时间内会发生睡眠,它已被弃用,您需要extern "C" { }
-include <unistd.h>
。
A third choice is to use select
and struct timeval
, as seen in http://source.winehq.org/git/wine.git/blob/HEAD:/dlls/ntdll/sync.c#l1204(this is how wine emulates Sleep
, which itself is just a wrapper for SleepEx
).
第三种选择是使用select
and struct timeval
,如http://source.winehq.org/git/wine.git/blob/HEAD:/dlls/ntdll/sync.c#l1204 中所见(这是 wine 模拟的方式Sleep
,它本身只是一个包装SleepEx
)。
Note: sleep
(lowercase 's'), whose declaration is in <unistd.h>
, is notan acceptable substitute, since its granularity is seconds, coarser than that of Windows' Sleep
(uppercase 's'), which has a granularity of milliseconds.
注意:(sleep
小写的 's'),其声明在 中<unistd.h>
,不是可接受的替代品,因为它的粒度是秒,比Sleep
粒度为毫秒的 Windows' (大写的 's')粗。
Regarding your second error, ___XXXcall
is a MSVC++-specific token (as are __dllXXX
, __naked
, __inline
, etc.). If you reallyneed stdcall, use __attribute__((stdcall))
or similar to emulate it in gcc.
关于第二个错误,___XXXcall
是一个MSVC ++ -特定的令牌(因为是__dllXXX
,__naked
,__inline
,等等)。如果您确实需要 stdcall,请使用__attribute__((stdcall))
或类似的方法在 gcc 中模拟它。
Note: unless your compile target is a Windows binary andyou're using Win32 APIs, use of or a requirement for stdcall
is A Bad Sign?.
注意:除非您的编译目标是 Windows 二进制文件并且您使用的是 Win32 API,否则使用或要求stdcall
是 A Bad Sign?。
回答by Eric Leschinski
How to use usleep in a C++ program on linux:
如何在 Linux 上的 C++ 程序中使用 usleep:
Put this in a file called s.cpp
把它放在一个名为 s.cpp
#include <iostream>
#include <unistd.h>
using namespace std;
int main() {
cout << "nitrate";
cout << flush;
usleep(1000000);
cout << "firtilizers";
return 0;
}
Compile it and run it:
编译并运行它:
el@defiant ~/foo4/40_usleep $ g++ -o s s.cpp
el@defiant ~/foo4/40_usleep $ ./s
nitratefirtilizers
It printed 'nitrate', waited 1 second, then printed 'firtilizers'
它打印“硝酸盐”,等待 1 秒,然后打印“过滤剂”
回答by alex
In my case it helped to write Sleep and NOT sleep - very strange, but worked!
就我而言,它有助于编写Sleep 而不是sleep - 非常奇怪,但有效!
回答by alex
#include <iostream>
#include <unistd.h>
using namespace std;
int main()
{
const long a=1000000;
long j;
cin >> j;
usleep(a*j);
puts("exit");
}
use usleep()
Insted of sleep and Don't forget to Include unistd.h
(Not cunistd
)
使用usleep()
Insted of sleep 并且不要忘记包含unistd.h
(Not cunistd
)