c++ 里用 alarm函数,该如何解决
c++ 里用 alarm函数
#include<unistd.h>
#include<signal.h>
#include<stdio.h>
#include<string>
#include <iostream>
using namespace std;
void handler(int)
{
string szBuf;
time_t timer = time(NULL);
strftime(szBuf, sizeof(szBuf), "%Y-%m-%d %H:%M:%S", localtime(&timer));
printf("%s/n", szBuf);
signal(SIGALRM, handler); //让内核做好准备,一旦接受到SIGALARM信号,就执行 handler
alarm(1800);
}
int main()
{
handler();
while(1)
;
return 0;
}
错误信息:
1.cpp: In function 'void handler(int)':
1.cpp:13: error: cannot convert 'std::string'
1.cpp:14: warning: cannot pass objects of non-
1.cpp: In function 'int main()':
1.cpp:8: error: too few arguments to function
1.cpp:26: error: at this point in file
------解决思路----------------------
你需要的不是 string szBuf, 而是 char szBuf[100]。
------解决思路----------------------
你的 handler 是有一个参数的啊,你的 main() 里的 hander(); 是什么意思?调用时要有一个实参的。
#include<unistd.h>
#include<signal.h>
#include<stdio.h>
#include<string>
#include <iostream>
using namespace std;
void handler(int)
{
string szBuf;
time_t timer = time(NULL);
strftime(szBuf, sizeof(szBuf), "%Y-%m-%d %H:%M:%S", localtime(&timer));
printf("%s/n", szBuf);
signal(SIGALRM, handler); //让内核做好准备,一旦接受到SIGALARM信号,就执行 handler
alarm(1800);
}
int main()
{
handler();
while(1)
;
return 0;
}
错误信息:
1.cpp: In function 'void handler(int)':
1.cpp:13: error: cannot convert 'std::string'
1.cpp:14: warning: cannot pass objects of non-
1.cpp: In function 'int main()':
1.cpp:8: error: too few arguments to function
1.cpp:26: error: at this point in file
------解决思路----------------------
你需要的不是 string szBuf, 而是 char szBuf[100]。
------解决思路----------------------
你的 handler 是有一个参数的啊,你的 main() 里的 hander(); 是什么意思?调用时要有一个实参的。