【发布时间】:2013-06-20 11:44:30
【问题描述】:
我正在尝试运行取自this simple example of a timer 的以下代码sn-p:
#include <sys/time.h>
#include <stdio.h>
int SetTimer(struct timeval &tv, time_t sec) {
gettimeofday(&tv, NULL);
tv.tv_sec += sec;
return 1;
}
int CheckTimer(struct timeval &tv, time_t sec) {
struct timeval ctv;
gettimeofday(&ctv, NULL);
if ((ctv.tv_sec > tv.tv_sec)) {
gettimeofday(&tv, NULL);
tv.tv_sec += sec;
return 1;
} else {
return 0;
}
}
int main() {
struct timeval tv;
SetTimer(tv, 5); //set up a delay timer
printf("start counting.\n");
while (1)
if (CheckTimer(tv, 5) == 1)
printf("Welcome to cc.byexamples.com\n");
return 0;
}
我收到以下错误:字段tv_sec 无法解析
我在网上搜索过,但似乎没有人给出具体答案。
我尝试了查看 sys/time.h 和 time.h 库的机会,但它们似乎都没有定义这种结构,但无论如何都被使用了。
我错过了任何图书馆吗?由于这个示例相当陈旧,是否有某些更改需要以不同的方式完成?我将不胜感激。
PS:我在 Ubuntu 11.10 和 g++ 4.6.1 下使用 Eclipse CDT Indigo。
【问题讨论】:
-
请在您的问题中包含代码(您指向的链接中至少有两个代码示例)并向我们显示确切的错误消息,包括报告的行号。第一个示例使用 g++ 为我编译(由于复制和粘贴问题,我将
&gt;更改为>)。