我正在尝试使用timeGetTime函数和其他一些时钟。但是我一直收到异常错误。我知道mayby程序的质量不是很好,但是我只是想让它工作。应该是单身。我希望你可以帮助我!
// The Clock header file
// The Clock API for meassuring time.
#include<Windows.h>
#include<WinBase.h>
#include<MMSystem.h>
class cTime
{
private:
double m_Ns;
double m_Ms;
double m_S;
public:
// Set Values
void SetN(double N) {m_Ns = N;}
void SetM(double M) {m_Ns = M;}
void SetS(double S) {m_Ns = S;}
// Get Values
double GetN() {return m_Ns;}
double GetM() {return m_Ms;}
double GetS() {return m_S;}
// GetTime functions
//int GetDiffrenceNs();
//int GetDiffrenceMs();
//int GetDiffrenceS();
};
class cClock
{
private:
cTime m_CurrentTime; // CurrentTime object
static cClock* m_pClock; // Pointer to only instance
cTime m_LastTime; // LastTime object
bool m_PerformanceCounter; // Set to true if the performance counter is available
double m_Frequency; // Tells the frequenty of the PerformanceCounter. The value that the PerformanceCounter will increase each second.
double m_CounterTime; // The Counter of the PerformanceCounter.
double m_Trillingstijd; // How long one count of the performance counter will take.
public:
static cClock* GetClock();
cTime CurrentTime(); // Get the CurrentTime.
cTime LastTime(); // Get the LastTime.
// Virtual destructor.
virtual ~cClock();
protected:
// Protected constructor.
cClock();
};
//时钟cpp文件
#include "Clock.h"
cClock* cClock::m_pClock = 0;
cClock* cClock::GetClock()
{
//BOOL perf_flag; // Timer Selection Flag
//double time_factor; // Time Scaling Factor
//LONGLONG last_time; // Previous timer value
//LONGLONG perf_cnt;
if (QueryPerformanceFrequency((LARGE_INTEGER *) &m_pClock->m_Frequency))
{
QueryPerformanceCounter((LARGE_INTEGER *) &m_pClock->m_CounterTime);
m_pClock->m_PerformanceCounter = true;
m_pClock->m_Trillingstijd=1.0/m_pClock->m_Frequency;
double LastedSeconds = m_pClock->m_CounterTime/m_pClock->m_Frequency;
m_pClock->m_LastTime.SetN(LastedSeconds*1000000);
m_pClock->m_LastTime.SetM(LastedSeconds*1000);
m_pClock->m_LastTime.SetS(LastedSeconds);
}
else
{
m_pClock->m_PerformanceCounter = false;
double LastedMiliseconds = timeGetTime();
m_pClock->m_LastTime.SetN(LastedMiliseconds*1000);
m_pClock->m_LastTime.SetM(LastedMiliseconds);
m_pClock->m_LastTime.SetS(LastedMiliseconds/1000);
}
return cClock::m_pClock;
}
cTime cClock::LastTime()
{
return m_LastTime;
}
cTime cClock::CurrentTime()
{
if(m_PerformanceCounter)
{
QueryPerformanceCounter((LARGE_INTEGER *) &m_CounterTime);
double LastedSeconds = m_CounterTime/m_Frequency;
m_CurrentTime.SetN(LastedSeconds*1000000);
m_CurrentTime.SetM(LastedSeconds*1000);
m_CurrentTime.SetS(LastedSeconds);
}
else
{
int LastedMiliseconds = timeGetTime();
m_CurrentTime.SetN(LastedMiliseconds*1000);
m_CurrentTime.SetM(LastedMiliseconds);
m_CurrentTime.SetS(LastedMiliseconds/1000);
}
m_LastTime = m_CurrentTime;
return m_CurrentTime;
}
这是我的主要工作,确实很简单,但是我只是试图使其工作而已,但是没有...
#include "Clock.h"
#include<iostream>
using namespace std;
int main()
{
cClock* Clock = cClock::GetClock();
cTime Test = Clock->CurrentTime();
cout << Test.GetN();
cout << Test.GetM();
cout << Test.GetS();
int temp;
cin >> temp;
return 0;
}
最佳答案
似乎方法cClock* cClock::GetClock()
使用m_pClock
而不初始化它(它仍然是0
)。
关于c++ - 制作Clock/Timer类时发生异常错误,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/7057726/