Template Problems

I got 3 classes:

Game
Loader
RawModel

in the loader class, I've got a function declaration:

template<int i> RawModel loadToVao(float (&positions)[i]);

in the loader.cpp file, I have got the definition:

template<int i>RawModel Loader::loadToVao(float (&positions)[i])
{
unsigned int vaoID = createVao();//createVao() is this class's function
storeDataInAttributeList(0, positions);//this function too
unBindVao();//this function too
return RawModel(vaoID, (sizeof(positions) / sizeof(float)) / 3);
}

now, in the game class, i've got this:

float data[] = {
1.0f, 0.0f

};

RawModel model = loader.loadToVao(data); //right on this line, it gives error




Here is the error :
error LNK2019: unresolved external symbol "public: class RawModel __thiscall Loader::loadToVao<18>(float (&)[18])" (??$loadToVao@$0BC@@Loader@@QAE?AVRawModel@@AAY0BC@M@Z) referenced in function "public: void __thiscall glGameEngine::Game::init(void)" (?init@Game@glGameEngine@@QAEXXZ)


Can someone please help me??

Note: class Game is in namespace glGameEngine (if that helps)


Templates differ from normal functions (and classes) in that the compiler needs to know the complete definition, at the point where the template is used; you can't just let the linker resolve it all the way you can with normal functions.

The most common way to resolve this is to have the complete definition of the function in the header file where it is declared. In this case, this means putting the complete definition of RawModel Loader::loadToVao() in the header file where the definition of Loader is.

Sir i tried this and it work awesomely cool!!!

can you just show me a way (if there is) to put the definitions inside the cpp file extension? Because i like organizing my work, i.e declarations one place, and definitions another place..


Thank you a lot sir...you saved my time
Last edited on
You cannot put template definitions in a .cpp file, because you cannot compile a template until you know how it is being instantiated.

I also like keeping definitions separate from declarations, so I create a file with a .tcpp extension (different from .cpp) for the definitions. And then at the bottom of the .h file, I #include the .tcpp file. This way, the entire template definition is available where instantiated in the code, but the look-and-feel of separating the declaration from the definition is preserved.

Edit: Corrected .cpp to .tcpp is included file.
Last edited on
templates go in the headers, whatever extensions you give them (if any). Many people keep their entire code in headers, such as almost every boost library. It's normal.
As an aside
1
2
3
4
template<int i>RawModel Loader::loadToVao(float (&positions)[i])
{
[...]
return RawModel(vaoID, (sizeof(positions) / sizeof(float)) / 3);

isn't that just return RawModel(vaoID, i/3);?
Last edited on
yes, sir you are right, it should be i/3. I was stupid, i guess, because first writing this function, I didnt have the template
Topic archived. No new replies allowed.