称为error的单例析构函数
Singleton destructor called error
#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 );
}
};
这是我的定时器单例代码。我想这样使用它:在玩家类的某处:
posX += vel * CTimer::getInstance().getDelta();
在主循环文件中:
void gameLoop(){
CTimer::getInstance().update();
...
}
但是我得到这个错误:
1>主要。obj:错误LNK2019:未解析的外部符号"private: __thiscall CTimer::~CTimer(void)"(??1CTimer@@AAE@XZ)引用函数"void _cdecl
public: static class getInstance & __cdecl CTimer::getInstance(void)'::
2':: ' dynamic atexit析构函数for 'instance " (void)"(? ? _Finstance@ ? 1 ? ? getInstance@CTimer@@SAAAV1@XZ@YAXXZ)
我认为这是因为主代码试图调用析构函数,循环结束后,我应该改为指针单例,但也许不是。你能告诉我怎么修吗?
当main
退出时,您的单例将被销毁(当然,如果它已初始化)。它的析构函数被调用。你必须实现它(至少是空的)。否则你的程序不能被链接
如果CTimer
类的唯一成员是time_t
变量,那么您不需要(未实现,因此链接错误)析构函数,复制构造函数和赋值操作符。只需注释这三行:这些函数将由编译器生成!
你在没有为析构函数写主体的情况下阻止了对象的析构,所以它会导致链接错误。请写析构函数体
~CTimer()
{} //code to free any resource
示例代码:http://ideone.com/TqtLVX view_edit_box
您必须为析构函数提供实现,因为它在main返回时被调用:
~CTimer() {}
第一个问题:您是否希望唯一的实例被销毁?如果你想销毁它,你必须提供一个主体的析构函数(即使它是空的),或者必须不要自己声明(这会让它公开),但是应该不成问题)。在c++ 11中,您还可以将其声明为=
default
,它告诉编译器生成它想要生成的内容通常为实现生成。
exit
或从main
)。 - 什么时候调用组成单元对象的析构函数
- 如果C++类在类方法中具有动态分配,但没有构造函数/析构函数或任何非静态成员,那么它仍然是POD类型吗
- 内联映射初始化的动态atexit析构函数崩溃
- 什么时候调用析构函数
- 单例类析构函数无法清理 (SDL_Quit) MinGW
- 如何析构单例实例,或者为什么以下代码适用于析构函数?
- 便携式C++单例 - 何时调用析构函数
- 单例:为什么不需要删除并且看不到析构函数调试消息
- 为什么在单例类中私有析构函数
- 析构函数调用表单不适当的库
- 具有使用 std::unique_ptr 的私有析构函数的单例
- 扩展具有私有构造函数和析构函数的单例类会给出编译时警告
- c++中,单例类的析构函数被再次调用
- lua绑定的单例类在析构函数中崩溃
- 虚拟析构函数用例
- 称为error的单例析构函数
- 从单例类调用继承类的析构函数
- 管理单例析构函数
- 为什么当我单击控制台上的关闭按钮时没有调用析构函数
- 单例析构函数