Initialize static template class data members

I'm having an issue with declaring and initializing in a small manageable way, the two alternatives which I know do work aren't exactly ideal.

1) Specialized template, providing every single combination of template parameters.
2) Changing a constant variables value.

The sample class layout is like so:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
//.h
class Resource
{
   typedef typename std::map<std::string, UINT> Label_Map;
   typedef Label_Map::iterator Label_Iterator;

   template<int ID>
      class Label
   {
   public:
      static Resource::Label_Map Labels;
      static int MaxValues;
   };
};


1
2
3
4
5
6
7
8
//.cpp
template<int ID> Resource::Data_Map Resource::Label<ID>::Labels;  //This works

//Using first method, too bulky
template<> int Resource::Label<Resource::Label_I>::MaxValues = 1;
template<> int Resource::Label<Resource::Property_I>::MaxValues = 1;
template<> int Resource::Label<Resource::Attribute_I>::MaxValues = 1;
template<> int Resource::Label<Resource::Effect_I>::MaxValues = 1;


Is there a reason why this works:
template<int ID> Resource::Data_Map Resource::Label<ID>::Labels;

Yet this does not:
template<int ID> int Resource::Label<ID>::MaxValues = 1;
"undefined reference to 'Resource::Label<X>::MaxValues"

If you don't understand quite what I'm asking, please ask, this has been haunting me for quite some time.
Last edited on
1
2
//.cpp
template<int ID> Resource::Data_Map Resource::Label<ID>::Labels;  //This works 
Are you sure? Did you try to access it in the main?

Put
1
2
template<int ID> Resource::Label_Map Resource::Label<ID>::Labels; 
template<int ID> int Resource::Label<ID>::MaxValues = 1;
in resource.h

If you want, you can provide specializations in resource.cpp

Edit: remove the typename in line 4. I'm not sure if you want Data_Map or Label_Map
Last edited on
Sorry, that was a typo.

Thankyou, placing the initialization in the header worked a treat.
Topic archived. No new replies allowed.