visual c++ 2005 下面 如何使用 CTime 类

AsuraLie 2007-09-25 10:23:27
#define _AFXDLL

#include <iostream>
#include <afxwin.h>
#include <conio.h>
#include <ctime>

using namespace std;



int hour,min,sec;


UINT Clock (LPVOID pParam)
{
while(true)
{
Ctime ctCurrentTime;
ctCurrentTime =CTime::GetTime();
min = ctCurrentTime.GetMinute();
sec = ctCurrentTime.GetSecond();
hour = ctCurrentTime.GetHour();
}
return 1;
}


void showTime()
{
cout<<endl;
Sleep(1000);
}

void main()
{
cout<<"press any key to quit"<<endl;
AfxBeginThread(Clock,NULL);
while(! _kbhit())
{
showTime();
}
}

程序代码如上,编译的时候错误如下,请问怎么解决,而且为什么在VC++2005和2008中都不能直接使用 #include <iostream.h>,而只能使用#include <iostream>?

------ 已启动生成: 项目: Timethread, 配置: Debug Win32 ------
正在编译...
TimeThread.cpp
WINVER not defined. Defaulting to 0x0502 (Windows Server 2003)
f:\my documents\visual studio 2005\projects\timethread\timethread\timethread.cpp(19) : error C2065: “Ctime”: 未声明的标识符
f:\my documents\visual studio 2005\projects\timethread\timethread\timethread.cpp(19) : error C2146: 语法错误 : 缺少“;”(在标识符“ctCurrentTime”的前面)
f:\my documents\visual studio 2005\projects\timethread\timethread\timethread.cpp(19) : error C2065: “ctCurrentTime”: 未声明的标识符
f:\my documents\visual studio 2005\projects\timethread\timethread\timethread.cpp(20) : error C2352: “ATL::CTime::GetTime”: 非静态成员函数的非法调用
e:\microsoft visual studio 8\vc\atlmfc\include\atltime.h(140) : 参见“ATL::CTime::GetTime”的声明
f:\my documents\visual studio 2005\projects\timethread\timethread\timethread.cpp(21) : error C2872: “min”: 不明确的符号
可能是“f:\my documents\visual studio 2005\projects\timethread\timethread\timethread.cpp(12) : int min”
或“min”
f:\my documents\visual studio 2005\projects\timethread\timethread\timethread.cpp(21) : error C2228: “.GetMinute”的左边必须有类/结构/联合
类型是“'unknown-type'”
f:\my documents\visual studio 2005\projects\timethread\timethread\timethread.cpp(22) : error C2228: “.GetSecond”的左边必须有类/结构/联合
类型是“'unknown-type'”
f:\my documents\visual studio 2005\projects\timethread\timethread\timethread.cpp(23) : error C2228: “.GetHour”的左边必须有类/结构/联合
类型是“'unknown-type'”
生成日志保存在“file://f:\My Documents\Visual Studio 2005\Projects\Timethread\Timethread\Debug\BuildLog.htm”
Timethread - 8 个错误,0 个警告
========== 生成: 0 已成功, 1 已失败, 0 最新, 0 已跳过 ==========
...全文
813 4 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
NKLoveRene 2007-09-25
  • 打赏
  • 举报
回复
还有一个,GetTime不是static函数,不能直接用类名加上::使用,必须用对象
NKLoveRene 2007-09-25
  • 打赏
  • 举报
回复

1.不能定义min这个参数,这个一查MSDN就知道了
2.最好用_beginthread()开线程
3.一个得到current time的代码:
int cMin,cHour,cSec;
time_t now = time(0);
struct tm * ptm = localtime(&now);
cMin = ptm->tm_min;
cSec = ptm->tm_sec;
cHour = ptm->tm_hour;
AsuraLie 2007-09-25
  • 打赏
  • 举报
回复
------ 已启动生成: 项目: Timethread, 配置: Debug Win32 ------
正在编译...
TimeThread.cpp
WINVER not defined. Defaulting to 0x0502 (Windows Server 2003)
f:\my documents\visual studio 2005\projects\timethread\timethread\timethread.cpp(20) : error C2352: “ATL::CTime::GetTime”: 非静态成员函数的非法调用
e:\microsoft visual studio 8\vc\atlmfc\include\atltime.h(140) : 参见“ATL::CTime::GetTime”的声明
f:\my documents\visual studio 2005\projects\timethread\timethread\timethread.cpp(21) : error C2872: “min”: 不明确的符号
可能是“f:\my documents\visual studio 2005\projects\timethread\timethread\timethread.cpp(12) : int min”
或“min”
生成日志保存在“file://f:\My Documents\Visual Studio 2005\Projects\Timethread\Timethread\Debug\BuildLog.htm”
Timethread - 2 个错误,0 个警告
========== 生成: 0 已成功, 1 已失败, 0 最新, 0 已跳过 ==========

出现了新的错误, 我把 GetTime()改为GetCurrentTime()也有错误,如下:


------ 已启动生成: 项目: Timethread, 配置: Debug Win32 ------
正在编译...
TimeThread.cpp
WINVER not defined. Defaulting to 0x0502 (Windows Server 2003)
f:\my documents\visual studio 2005\projects\timethread\timethread\timethread.cpp(21) : error C2872: “min”: 不明确的符号
可能是“f:\my documents\visual studio 2005\projects\timethread\timethread\timethread.cpp(12) : int min”
或“min”
生成日志保存在“file://f:\My Documents\Visual Studio 2005\Projects\Timethread\Timethread\Debug\BuildLog.htm”
Timethread - 1 个错误,0 个警告
========== 生成: 0 已成功, 1 已失败, 0 最新, 0 已跳过 ==========
xugang_2001 2007-09-25
  • 打赏
  • 举报
回复
楼主,你的代码犯了小错误- -#,你把Ctime ctCurrentTime;的t改称大写的看看.

65,187

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧