This is driving me round the bend at the moment. As a homebrew exercise I wanted to template a recursive call in a class. in the .h file I have:
template <typename T1>
class BinaryTree
{
public:
BinaryTree(T1 element);
~BinaryTree();
BinaryTree* addLeftLeaf(BinaryTree<T1>* node);
etc…
then in the .cpp
template <typename T1> BinaryTree* BinaryTree<T1>::addLeftLeaf(BinaryTree<T1>* node)
{
return node;
}
I’ve tried seemingly loads of ideas but thus far nothing. Just errors like error C2955: ‘BinaryTree’ : use of class template requires template argument list
Any suggestions would be kindly appreciated.
Thanks
Mark
In the source file, you need to specify
BinaryTree<T1>instead of justBinaryTree. i.e.You can only refer to a template without its parameter list within the class/struct body.
Beware also that generally it’s a bad idea to have non-specialized templates in
.cppfiles because that means people won’t be able to implicitly instantiate them (without#includeing the.cpp).As a rule of thumb, only specialized templates should go in the
.cpp. Non-specialized templates should live in the.h.If you know what you are doing and plan on manually instantiating the templates then by all means put the body in the
.cpp, but generally that isn’t what people do.