C++计时器类不编译,我不明白为什么

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

本文关键字:明白 为什么 编译 计时器 C++      更新时间:2023-10-16

有一个很棒的c++定时器类,我想在Windows上使用:

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

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

然而,当我尝试在VS2010上构建它时,它告诉我例如"endCount"没有声明。

然而,我可以看到它是。

"#ifded WIN32"部分没有变灰,所以我猜这部分是"in reach"并执行。

#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

在VC中,#include "stdafx.h"之前的所有内容都被忽略。stdafx.h后包括Timer.hstdlib.h

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

作为替代方法,请禁用包含预编译的头文件。

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

设置"创建/使用预编译头"为"不使用预编译头"