0

我想在 Windows 上使用一个很棒的 C++ 计时器类:

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

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

但是,当我尝试在 VS2010 上构建它时,它告诉我例如“endCount”没有声明。

但是,我可以看到它是。

“#ifded WIN32”部分没有变灰,所以我猜这部分是“触手可及”并被执行。

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

我想总体上有些东西坏了,但我看不出是什么。

有人可以看看这个项目,看看为什么会出错?

这是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;
}

这是标题:

#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
4

3 回答 3

3

在VC中,之前的一切都#include "stdafx.h"被忽略了。包括Timer.hstdlib.h之后stdafx.h

于 2013-04-04T06:50:14.307 回答
2

#include "stdafx.h"应该始终是 .cpp 文件中的第一个包含。在编译器跳过它之前包含它,因为它假定这些是预编译头文件的一部分。

于 2013-04-04T06:51:51.707 回答
0

作为替代方案,禁用包含预编译的标头。

在项目属性 -> C/C++ -> 预编译头文件中。

将“创建/使用预编译头”设置为“不使用预编译头”。

于 2013-04-04T07:22:07.723 回答