0
#pragma once
#include <time.h>        

class CTimer
{
    time_t _last;
    CTimer() { _last = time( NULL ); }
    CTimer(const CTimer &);
    CTimer& operator=(const CTimer&);
    ~CTimer();
public:
    static CTimer& getInstance(){        
        static CTimer instance;
        return instance;
    }

    float getDelta(){
        time_t now = time( NULL );
        float delta = (float)(now - _last);     
        return delta;
    }
    //should be called at the beginning of rendering function
    void update(){
        _last = time( NULL );
    }
};

This is my Timer singleton code. I wanted to use it like that: Somewhere in player class:

posX += vel * CTimer::getInstance().getDelta();

And in main loop file:

void gameLoop(){
CTimer::getInstance().update();
...
}

But I get this error:

1>Main.obj : error LNK2019: unresolved external symbol "private: __thiscall CTimer::~CTimer(void)" (??1CTimer@@AAE@XZ) referenced in function "void _cdecl public: static class getInstance & __cdecl CTimer::getInstance(void)'::2'::`dynamic atexit destructor for 'instance''(void)" (??_Finstance@?1??getInstance@CTimer@@SAAAV1@XZ@YAXXZ)

I think its because main code tries to call destructor, after loop ends and I should change to pointers singleton, but maybe not. Could you tell me how to fix this?

Machiaweliczny
  • 572
  • 1
  • 6
  • 16

5 Answers5

3

Your singleton is destructed when main exits (if it was initialized of course). So it's destructor is called. You have to implement it (at least empty). Otherwise your program can not be linked

Andrew
  • 24,218
  • 13
  • 61
  • 90
1

If the only member of your CTimer class is the time_t variable, then you don't need the (not implemented, hence the linking error) destructor, the copy constructor and the assignment operator. Just comment those three lines: these functions will be generated by the compiler !

Liviu
  • 1,859
  • 2
  • 22
  • 48
  • If OP needs the destructor to be private, then commenting it out won't do. – juanchopanza May 21 '13 at 11:55
  • 1
    Furthermore, a singleton should not have a copy constructor or an assignment operator. – juanchopanza May 21 '13 at 12:05
  • True, my solution solves only the linkage problem(s), the best solution is to have them private, with empty implementations, to forbid other objects creation. – Liviu May 21 '13 at 12:10
1

You have to provide an implementation for the destructor, since it is called when main returns:

~CTimer() {}
juanchopanza
  • 223,364
  • 34
  • 402
  • 480
1

You are blocking destruction of object without writing body for destructor so it is leading to linking error. Please write body of destructor

~CTimer()
    {} //code to free any resource

Example code: http://ideone.com/TqtLVX#view_edit_box

shivakumar
  • 3,297
  • 19
  • 28
0

First: do you want the unique instance to be destructed or not? If you want it to be destructed, you must either provide a body for your destructor somewhere (even if it is empty), or you must not declare it yourself (which would make it public, but that shouldn't be a problem). In C++11, you can also declare it = default, which tells the compiler to generate what it would normally generate for the implementation.

Most of the time, however, you don't want singleton's to be destructed. One of the main reasons for using a singleton is to solve order of initialization problems; destructing it leaves the door open to order of destruction issues. The usual idiom would be to use a static pointer to the singleton, testing whether it is null in the instance function, and allocating a new instance if it is. (If I can believe the comment, which suggests that this class is only used during rendering, it probably doesn't matter, since you won't be doing anything with the singleton once you've called exit or returned from main.)

James Kanze
  • 150,581
  • 18
  • 184
  • 329