C++计时器类不编译,我不明白为什么
C++ timer class not compiling, I don't see why
有一个很棒的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.h
和stdlib.h
#include "stdafx.h"
应该始终是.cpp文件中的第一个包含。编译器会跳过它之前的include,因为它假定这些是预编译头文件的一部分。
作为替代方法,请禁用包含预编译的头文件。
项目属性-> C/c++ ->预编译头文件。
设置"创建/使用预编译头"为"不使用预编译头"
相关文章:
- 我不明白为什么我声明一个空的内部结构并将其传递给构造函数
- 而循环:简单的除法程序输出零,不明白为什么
- 我不明白为什么我的代码不起作用并且需要更长的时间来运行
- 什么是非营利组织???我的问题是我不明白为什么有人会使用它
- 我正在尝试用 c++ 制作菜单,但不明白为什么它不循环
- 我的程序不适用于 strcat - 我似乎不明白为什么?
- 我不明白为什么会编译
- 我不明白为什么"Derived1"需要与"Derived3"相同的内存量
- 不明白为什么动态编程在这里不起作用
- 我收到"undefined reference"错误,我不明白为什么(C++ OO)
- 我不明白为什么这个行之有效
- 我不明白为什么。双倍的问题
- 不明白为什么这个简单的递归开始工作然后崩溃
- 我明白为什么,但是虚拟函数/VTable 究竟如何允许通过指针访问正确的函数?
- 我不明白为什么会出现语法错误
- 我不明白为什么当我按位或短字符时得到这个结果
- 每次运行此程序时,我都会不断获得Segfault,但我不明白为什么
- 我不明白为什么 for 循环变得无限
- 我不明白为什么我会收到语法错误或如何解决它
- 我收到此错误:错误 c2064:term 的计算结果不是采用 0 个参数的函数,但我不明白为什么