Archived

This topic is now archived and is closed to further replies.

chbfiv

Templates and Abstract classes

Recommended Posts

quote:
error C2259: ''Engine'' : cannot instantiate abstract class
Does this mean I''m going to need to make a second template Singleton for an abstract class? Singleton.h //Common Template
template<typename Type> class Singleton {
public:
	inline static Type* Instance() {
		if(!pInstance){
			pInstance = new Type();
			atexit(Cleanup);
		}
		return pInstance;
	}
	inline static void Cleanup() {
		if(!pInstance) {
		delete pInstance;
		pInstance = NULL;
		}
	}
protected:
	Singleton(Type* pInst)  { pInstance = pInst }
	Singleton() { }
	Singleton(const Singleton&) { }
	virtual ~Singleton() { }
private:
	static Type* pInstance;
};
template <typename Type>
Type* Singleton<Type>::pInstance = 0;
System.h //Interface used by alot of classes
class ISystem {
public:
	ISystem() :sLabel("System"),sError(":: Exception!") { }
	virtual ~ISystem() { }
	virtual int Init() = 0;
	virtual void Start() = 0;
	virtual void Halt() = 0;
	virtual void ThrowError() = 0;
protected:
private:
	string sLabel;
	string sError;
};

Engine.h //Main class, should be both a System and a Singleton
#include "Singleton.h"
#include "System.h"

class Engine : public Singleton<Engine>,public ISystem {
	friend class Singleton;
public:
	~Engine();
        Init();
        Start();
        Halt();
protected:
	Engine();
	Engine(const Engine&) { };
	Engine& operator= (const Engine&);	
private:

};
static Engine *pEngine = Engine::Instance();

Share this post


Link to post
Share on other sites
This has nothing to do with templates, and everything to do with the fact that you aren''t properly implementing the pure-virtual functions defined in IEngine in your Engine class. (they don''t have listed return types, which means they return "int" implicitly, and you haven''t put int ThrowError() at all.)


How appropriate. You fight like a cow.

Share this post


Link to post
Share on other sites