Tuesday, 27 August 2013

Using static maps within a class inside different functions:

Using static maps within a class inside different functions:

I have a class like this:
class SelectorFactory
{
public:
static std::map<std::string,Int> _creator;
static void registerCreator(std::string& name,int value)
{
//static std::map<std::string,Selector*> _creator;
if(_creator.end() != _creator.find(name))
{
std::cout << "Selector already registered \n";
}
else
{
std::cout << "Entering " <<name<<" in register: \n";
_creator[name]=value;
}
}
static int createSelector(std::string selectorName)
{
//static std::map<std::string,Selector*> _creator;
std::map< std::string , int >::iterator
mapIter=_creator.find(selectorName);
if(mapIter==_creator.end())
{
std::cout<<selectorName<<" Not found in the Map \n" ;
}
else
{
int selector= mapIter->second;
if(selector==NULL)
{
std::cout<<selectorName + "Selector does not exists \n" ;
return 0;
}
return selector;
}
}
};
If I uncomment the commented lines above, code is getting compiled but
it's not returning any value from createSelector function which is quite
obvious.But if I keep them commented, I am getting error as "_creator was
not declared in this scope" inside both the functions. What should I do to
rectify this issue.

No comments:

Post a Comment