0

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,int> _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,int> _creator;
        std::map< std::string , int >::iterator mapIter=_creator.find(selectorName);
        if(mapIter==_creator.end())
        {
            std::cout<<selectorName<<" Not found in the Map \n" ;
            return 0;
        }
        else
        {
            int selector= mapIter->second;
            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.

3
  • 1
    Your code is ill-formed. createSelector can flow off the end without returning anything. You should fix that first. Commented Aug 27, 2013 at 9:51
  • 1
    What is Int, is it the same as int? Why are you comparing an int to NULL? selector is not a pointer. The code cannot compile with the lines uncommented, because Selector is not declared. Please fix your code to show the real problem. Commented Aug 27, 2013 at 10:23
  • The current version has no compile problems for me. Commented Aug 27, 2013 at 10:40

2 Answers 2

1

In order to have -creator instantiated, you must provide a definition for it. Currently, you have only a declaration.

class SelectorFactory
{
    static std::map<std::string,Int> _creator;
};
std::map<std::string,Int> SelectorFactory::_creator;
Sign up to request clarification or add additional context in comments.

2 Comments

But I have my function defined inside the class itself where I need to use this map.
@sajal, that doesn't matter, you still have to define the static member (however forgetting to do so would not cause the "_creator was not declared in this scope" error)
1

SelectorFactory.h :

#ifndef __SELECTOR_FACTORY__H__
#define __SELECTOR_FACTORY__H__
#include <string>
#include <map>

class SelectorFactory
{
public: 
    static void registerCreator(std::string& name,int value); 
    static int createSelector(std::string selectorName);
private:              // !!!!!!!!! NOT PUBLIC!!!   >:(
    static std::map<std::string,int> _creator;   

};
#endif // __SELECTOR_FACTORY__H__

SelectorFactory.cpp :

#include "SelectorFactory.h"
#include <iostream>

std::map<std::string,int> SelectorFactory::_creator; 

void SelectorFactory::registerCreator(std::string& name,int value)
{
    if(_creator.end() != _creator.find(name))
    {
        std::cout << "Selector already registered \n";
    }
    else
    {
        std::cout << "Entering " <<name<<" in register: \n";
        _creator[name]=value;
    }
}   

int SelectorFactory::createSelector(std::string selectorName)
{
    std::map< std::string , int >::iterator mapIter=_creator.find(selectorName);
    if(mapIter==_creator.end())
    {
        std::cout<<selectorName<<" Not found in the Map \n" ;
        return 0;
    }
    else
    {
        int selector= mapIter->second;
        return selector;
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.