Q:

C++ Template parameter deduction from array dimensions

i need your help with the parameterization of a template with an int value.
Thats how it looks like:
SpriteSwitcher.h:
template<int N >
    bool initWithFilesArray(std::string (&fileNames)[N],int width, const CCPoint position);

SpriteSwitcher.cpp:
    template<int N >
    bool SpriteSwitcher::initWithFilesArray(std::string (&fileNames)[N], int width, const CCPoint position)
    {
      return true;
    }

and then i try to initialize the class SpriteSwitcher with my initWithFileArray function
std::string g[2] = {"hello", "world"};
SpriteSwitcher *s = new SpriteSwitcher();
s->initWithFilesArray(g, visibleSize.width, origin);

and I get the following error:
error lnk2019 unresolved external symbol ""public: bool __thiscall SpriteSwitcher::initWithFilesArray<2>(class std::basic_string,class std::allocator > (&)[2],int,class cocos2d::CCPoint)" (??$initWithFilesArray@$01@SpriteSwitcher@@QAE_NAAY01V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@HVCCPoint@cocos2d@@@Z)" in Funktion ""public: virtual bool __thiscall MenuScreen::init(void)" (?init@MenuScreen@@UAE_NXZ)".
Can anyone help me, and tell me where the problem is? 

A:

Your function is a template, which means the definition must be visible at the instantiation point.  This means either moving your definition into your header file or explicitly instantiating it for all the values of N you expect to occur.  If you expect N to always be less than about 10 or so, this might be practical.  Otherwise, you'll need to go with putting the definition in the header file so it can be instantiated when it is used with the right template parameters.

