简体   繁体   中英

Class definition in a header c++

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.

You forgot to provide a definition for the destructor:

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

Notice, however, that you do not need to provide a destructor explicitly in this case: the compiler will generate one implicitly for you. Simply omit it.

You are missing an implementation for the destructor:

~ElapsedTime() { ..... }

If the destructor doesn't do anything, and is not virtual , you can remove the declaration instead.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM