制作Clock / Timer类时出现异常错误

时间:2011-08-14 15:30:07

标签: c++ class exception

我正在尝试使用timeGetTime函数和其他一些函数创建一个Clock。但我不断收到异常错误。我知道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;
}

2 个答案:

答案 0 :(得分:2)

似乎方法cClock* cClock::GetClock()使用m_pClock而未初始化它(它仍然是0)。

答案 1 :(得分:2)

您永远不会创建cClock的实例,只是访问指向一个的空指针。

如果你真的认为单身是一个好主意,那么cClock::GetClock()必须创建一个,如果它还不存在;

cClock* cClock::GetClock()
{
    if (!m_pClock) {
        m_pClock = new cClock;
    }

    // remainder of function

    return m_pClock;
}

请注意,这不是线程安全的,并且还会引入内存泄漏。单身人士很难用C ++实现,除非有真正的理由想要单身,否则最好避免使用。我会将GetClock()的逻辑移动到公共构造函数中,并允许客户端代码在其认为合适时创建和销毁时钟对象。