At the bottom of Item.cpp add the line:
template class Item<int>;
That tells the compiler to build code for that particular specialisation, otherwise when the compiler hits that file it doesn't know what's needed.
At the bottom of Item.cpp add the line:
template class Item<int>;
That tells the compiler to build code for that particular specialisation, otherwise when the compiler hits that file it doesn't know what's needed.
Ya that solves the problem but introduces a new one - It's not usable. I can expect the users of the Item class to type the class name in Item.cpp. There must be an elegant solution isn't it?. Moreover what if i plan to move the implemention to a .so/.dll file. Then what will they do .Originally Posted by Weaver
If the implementation is kept in the .h The user even need not bother about the declaration - template class Item<int>;. But now they get to see the implementation and may be even change it .
I want the implementation to be put in the .cpp and leave the .cpp file intact. so that others can't mess things up.
We can't solve problems by using the same kind of thinking we used when we created them
Well you can't stop people reimplementing your functions... they can always take your header and implement a new library =)
The problem with templates is that they're only generated "on demand". ie the specialisation of the function is only ever created if it's referred to. When the compiler sees Item.cpp it doesn't know what types of baseFunction() to generate. When it sees a reference it it in another cpp file, it can't see where the function is defined to create a specialisation.
Hopefully this explains it better:
http://www.parashift.com/c++-faq-lit...html#faq-35.12
If you don't know ahead of time what types you'll need, then the best you can do is #include "Item.cpp" at the bottom of Item.h
Thanks That link solved all my doubts
We can't solve problems by using the same kind of thinking we used when we created them
Bookmarks