Jump to content
  • Advertisement
Sign in to follow this  
KH

Register Factories - static map in templated class

This topic is 4832 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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;
};

Share this post


Link to post
Share on other sites
Advertisement
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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!