首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

visual c++ 2005 下面 怎么使用 CTime 类

2012-03-02 
visual c++ 2005 下面 如何使用 CTime 类#define_AFXDLL#includeiostream#includeafxwin.h#includeco

visual c++ 2005 下面 如何使用 CTime 类
#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   已跳过   ==========



[解决办法]
楼主,你的代码犯了小错误- -#,你把Ctime ctCurrentTime;的t改称大写的看看.
[解决办法]

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;

[解决办法]
还有一个,GetTime不是static函数,不能直接用类名加上::使用,必须用对象

热点排行