Re: Pure virtual functions in Template class?
YellowMaple wrote:
Sorry, for the sake of brevity I excluded most of the code. Here is
an accurate representation:
......
class FontRegistry
: public Registry<FTFont>
, public Singleton<FontRegistry>
{
public:
~FontRegistry();
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Your problem is here. This is a virtual destructor and you have not
implemented it.
Please take a few more minutes to make the code compilable (cut-n-paste)
so that the code you give shows the error without others having to guess
what are the other errors about.
This is the code I ended up with, I'm not sure it truly represents your
problem.
Another pointer, avoid #defines like the one you use for FONT_TYPE.
//in Singleton.h:
#include <string>
#ifndef __SINGLETON_H
#define __SINGLETON_H
template <typename T>
class Singleton
{
public:
static T& getInstance(void)
{
if(m_pInstance == NULL)
m_pInstance = new T;
return *m_pInstance;
}
protected:
Singleton() { }
Singleton(const Singleton&);
virtual ~Singleton() { }
Singleton& operator=(const Singleton&);
private:
static T* m_pInstance;
};
template <typename T>
T* Singleton<T>::m_pInstance = NULL;
#endif
//In Registry.h
#ifndef __REGISTRY_H
#define __REGISTRY_H
#include <map>
#include <string>
#include <algorithm>
//#include <Design/Subject.h>
//#include <Utility/Globals.h>
//#include <Design/Singleton.h>
struct delete_ptr
{
template <typename T>
void operator() (const T& obj) const
{
delete obj.second;
}
};
class Subject {};
template <typename ResourceType>
class Registry
: public Subject
{
public:
typedef std::string resource_type;
ResourceType* create(std::string);
void clear();
bool empty() { return m_list.empty(); }
virtual resource_type getType() = 0;
protected:
Registry() { }
virtual ~Registry() { clear(); }
Registry(const Registry&);
Registry& operator=(const Registry&);
virtual ResourceType* loadResource(std::string) = 0;
typedef std::map<resource_type, ResourceType*> registry_list;
registry_list m_list;
};
template <typename ResourceType>
ResourceType* Registry<ResourceType>::create(std::string name)
{
return NULL;
}
template <typename ResourceType>
void Registry<ResourceType>::clear()
{
//notifyAll();
//for_each(m_list.begin(), m_list.end(), delete_ptr());
//m_list.clear();
}
#endif
//and finally, FontRegistry.h:
#ifndef __FONTREGISTRY_H
#define __FONTREGISTRY_H
#include <string>
//#include <FTGLPixmapFont.h>
//#include <Resources/Registry.h>
#define FONT_TYPE ".ttf"
class FTFont {};
class FontRegistry
: public Registry<FTFont>
, public Singleton<FontRegistry>
{
public:
~FontRegistry() {};
resource_type getType() { return FONT_TYPE; }
protected:
FTFont* loadResource(std::string) { return NULL; }
private:
FontRegistry() { }
FontRegistry(const FontRegistry&);
FontRegistry operator=(const FontRegistry&);
class FTGLPixmapFont {};
typedef FTGLPixmapFont FontType;
friend class Singleton<FontRegistry>;
};
#endif
int main()
{
FTFont* ftgl_font = FontRegistry::getInstance().create( "" );
}