Register Factories - static map in templated class

Started by
3 comments, last by KH 18 years, 8 months ago
I have taken a look at the article Industrial Strength Pluggable Factory found in C++ Report from 99, in the end the author describes how they made a templated version of the Maker class. Im having problems trying to implement this, the problem is with the static map containing references instances of each factory along with a name. I have tried a number of different ways in the static newObject method to access the static map and retrieve the reference to the named factory instance but with no luck. With this code I get a LNK2020 error: error LNK2020: unresolved token (0A000075) ?registry@?$Factory@VKaross@@@@2V?$map@V.....

class AudiTTFactory : public KarossFactory
{
private:
	static const AudiTTFactory registerThis;
	AudiTTFactory() : KarossFactory("AudiTT") {};
	Kaross* makeObject()const
	{
		return new AudiTT();
	}
};

class KarossFactory : public Factory<Kaross>
{
public:
	KarossFactory(const string &className) : Factory<Kaross>(className) {};
};

template 
<class Object> class Factory
{
public:
typedef Factory<Object>* FactoryPtr; 
typedef map<string, FactoryPtr> FactoryMap;	
static FactoryMap registry;

virtual ~Factory(){};

Factory(const string& className)
{
  registry->insert( std::make_pair(className, this) );  	   
};

static Object* newObject(const string& className)
{
Factory<Object> *maker = Factory<Object>::registry.find(className)->second;
return maker->makeObject();
};

virtual Object* makeObject() const=0;
};
Advertisement
what was the entire linker error?
Anything posted is personal opinion which does not in anyway reflect or represent my employer. Any code and opinion is expressed “as is” and used at your own risk – it does not constitute a legal relationship of any kind.
PluggableFactory error LNK2020: unresolved token (0A000075) ?registry@?$Factory@VKaross@@@@2V?$map@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@PAV?$Factory@VKaross@@@@U?$less@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@2@V?$allocator@U?$pair@$$CBV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@PAV?$Factory@VKaross@@@@@std@@@2@@std@@A
Looks like you need to add:
template < typename Object >
typename Factory< Object >::FactoryMap Factory< Object >::registry;
You've declared your registry object, but you haven't actually defined it.

Enigma
Thanks for the help
Your code to solve the linker error + a definition of one AudiTTFactory object was needed to get it running and to fill the registry with an instance of the factory.

This topic is closed to new replies.

Advertisement