tmighty
tmighty

Reputation: 11425

C++ timer class not compiling, I don't see why

There is a great timer class for C++ that I wanted to use on Windows:

http://www.songho.ca/misc/timer/timer.html

http://www.songho.ca/misc/timer/files/timer.zip

However, when I try to build it on VS2010, it tells me that for example "endCount" is not declared.

However, I can see that it is.

The section "#ifded WIN32" is not greyed out, so I guess this section is "in reach" and executed.

#ifdef WIN32
    LARGE_INTEGER frequency;                    // ticks per second
    LARGE_INTEGER startCount;                   //
    LARGE_INTEGER endCount;                     //

I guess there is something broken in general, but I don't see what.

Can somebody perhaps take a look at the project and see why might the error?

Here is a part of the cpp:

#include "Timer.h"
#include <stdlib.h>
#include "stdafx.h"
///////////////////////////////////////////////////////////////////////////////
// constructor
///////////////////////////////////////////////////////////////////////////////
Timer::Timer()
{
#ifdef WIN32
    QueryPerformanceFrequency(&frequency);
    startCount.QuadPart = 0;
    endCount.QuadPart = 0;
#else
    startCount.tv_sec = startCount.tv_usec = 0;
    endCount.tv_sec = endCount.tv_usec = 0;
#endif

    stopped = 0;
    startTimeInMicroSec = 0;
    endTimeInMicroSec = 0;
}

And this is the header:

#ifndef TIMER_H_DEF
#define TIMER_H_DEF

#ifdef WIN32   // Windows system specific
#include <windows.h>
#include "stdafx.h"
#else          // Unix based system specific
#include <sys/time.h>
#endif


class Timer
{
public:
    Timer();                                    // default constructor
    ~Timer();                                   // default destructor

    void   start();                             // start timer
    void   stop();                              // stop the timer
    double getElapsedTime();                    // get elapsed time in second
    double getElapsedTimeInSec();               // get elapsed time in second (same as getElapsedTime)
    double getElapsedTimeInMilliSec();          // get elapsed time in milli-second
    double getElapsedTimeInMicroSec();          // get elapsed time in micro-second


protected:


private:
    double startTimeInMicroSec;                 // starting time in micro-second
    double endTimeInMicroSec;                   // ending time in micro-second
    int    stopped;                             // stop flag 
#ifdef WIN32
    LARGE_INTEGER frequency;                    // ticks per second
    LARGE_INTEGER startCount;                   //
    LARGE_INTEGER endCount;                     //
#else
    timeval startCount;                         //
    timeval endCount;                           //
#endif
};

#endif // TIMER_H_DEF

Upvotes: 0

Views: 258

Answers (3)

Arun
Arun

Reputation: 2102

As an alternative, disable inclusion of precompiled header.

In Project properties -> C/C++ -> Precompiled Headers.

Set "Create/Use Precompiled Header" to "Not Using Precompiled Headers".

Upvotes: 0

Bo Persson
Bo Persson

Reputation: 92391

The #include "stdafx.h" should always be the first include in the .cpp file. Includes before it will be skipped by the compiler, because it assumes those are part of the precompiled header.

Upvotes: 2

Henrik
Henrik

Reputation: 23324

In VC, everything before #include "stdafx.h" is ignored. Include Timer.h and stdlib.h after stdafx.h.

Upvotes: 3

Related Questions