0

I have this code:

#include <time.h> 
class ElapsedTime
{   
    time_t _startTime;
public:
    ElapsedTime(void){
        time(&_startTime);  
    }

    double MiliSecond()
    {
        time_t endTime;
        time(&endTime);
        return difftime(_startTime,endTime) * 1000;
    }

    ~ElapsedTime(void);
};

and I used it inside my c++ code. It compiles but generate error during linking as the linker says it can not find the elapsetime definition.

How can I define a class completely in an H file? For this simple class, I don't want to have a .h and a .cpp.

4

2 回答 2

7

您忘记为析构函数提供定义:

~ElapsedTime(void) { }
//                 ^^^

但是请注意,在这种情况下您不需要显式提供析构函数:编译器将为您隐式生成一个析构函数。简单地省略它。

于 2013-05-23T12:28:20.570 回答
3

您缺少析构函数的实现:

~ElapsedTime() { ..... }

如果析构函数没有做任何事情,并且 is not virtual,您可以改为删除声明。

于 2013-05-23T12:28:26.120 回答