static variable initialisation in .hpp



  • Hi guys,

    i am programming a template class and therefore i have to use a .hpp file.
    My question is, how can i initialize the static variable filters in line 34 with NULL?

    template<typename TF>
            class AbstractFilter : public IFilter<TF>
            {
            public:
    
                template<typename T> static IFilter<TF>* createTemplate() {return new T;}
    
                struct FilterFactory
                {
                    typedef boost::unordered_map<std::string, IFilter<TF>*(*)()> FilterMap;
    
                    static IFilter<TF>* createFilterInstance(const std::string &name)
                    {
    
                        typename FilterMap::iterator it = getMap()->find(name);
                        if(it != getMap()->end())
                            return it->second();
                        else
                        {
                            //fprintf(stderr, "Could not find filter %s", name);
                            return NULL;
                        }
                    }
    
                protected:
                    static FilterMap* getMap()
                    {
                        if(!filters)
                            filters = new FilterMap;
                        return filters;
                    }
    
                private:
                    static FilterFactory::FilterMap *filters = NULL; //Error here
            };
    

    If i set it to NULL as indicated in the code, the Compiler gives me the following error:

    error: invalid in-class initialization of static data member of non-integral type ‘ccns::util::AbstractFilter<std::basic_string<char> >::FilterFactory::FilterMap* {aka boost::unordered::unordered_map<std::basic_string<char>, ccns::util::IFilter<std::basic_string<char> >* ()(), boost::hash<std::basic_string<char> >, std::equal_to<std::basic_string<char> >, std::allocator<std::pair<const std::basic_string<char>, ccns::util::IFilter<std::basic_string<char> > ()()> > >}’

    best regards,
    daniel_c++



  • If you want to keep this static things, you could move the filter member variable to a function local variable into getMap() .

    static FilterMap* getMap()
    {
      static FilterMap *filters = NULL;
      if(!filters)
        filters = new FilterMap;
      return filters;
    }
    

    but then, you could...

    static FilterMap* getMap()
    {
      static FilterMap filters;
      return &filters;
    }
    

    or even with references...

    static FilterMap& getMap()
    {
      static FilterMap filters;
      return filters;
    }
    

    Edit:
    There is a closing brace missing - so, assuming filters is a member of FilterFactory .



  • Hello daniel.

    You have to initialize the static memberpointer outside of the class.
    [code="cpp"]
    template<typename TF>
    class AbstractFilter : public IFilter<TF>
    {
    public:

    template<typename T> static IFilter<TF>* createTemplate() {return new T;}

    struct FilterFactory
    {
    typedef boost::unordered_map<std::string, IFilter<TF>()()> FilterMap;

    static IFilter<TF>* createFilterInstance(const std::string &name)
    {

    typename FilterMap::iterator it = getMap()->find(name);
    if(it != getMap()->end())
    return it->second();
    else
    {
    //fprintf(stderr, "Could not find filter %s", name);
    return NULL;
    }
    }

    protected:
    static FilterMap* getMap()
    {
    if(!filters)
    filters = new FilterMap;
    return filters;
    }

    private:
    static FilterFactory::FilterMap *filters; //without initialization
    };

    static AbstractFilter::FilterFactory::FilterMap *filters = NULL;



  • thank you very much guys!



  • to initialize schrieb:

    Hello daniel.

    You have to initialize the static memberpointer outside of the class.
    ...

    See the code, its a template and won't work this way.



  • But in this way it works...

    class DummyTest {};
    
    template <typename T>
    class AbstractFilter 
    {
    public:
    	struct FilterFactory
    	{
    		typedef DummyTest FilterMap;
    	};
    
    private:
    	static typename FilterFactory::FilterMap* filters;
    };
    
    template <typename T>
    typename AbstractFilter<T>::FilterFactory::FilterMap* AbstractFilter<T>::filters = NULL;
    
    int main()
    {
    	return 0;
    }
    


  • Indeed...


Anmelden zum Antworten