c++ 里用 alarm函数

C++语言 码拜 9年前 (2015-10-24) 2282次浏览
#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

解决方案:5分
你需要的不是 string szBuf, 而是 char szBuf[100]。
解决方案:20分
你的 handler 是有一个参数的啊,你的 main() 里的 hander(); 是什么意思?调用时要有一个实参的。

CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明c++ 里用 alarm函数
喜欢 (0)
[1034331897@qq.com]
分享 (0)