Reputation: 8058
I have a Binary-Tree class template with several public and private member functions. When the member functions are both defined and implemented inside the class template, it compiles correctly and functions as expected. However, when I move those functions outside of the main class template, I get a multiplicity of errors.
According to this reference, class template functions cannot be made external because the templates are compiled at run-time. http://www.codeguru.com/forum/archive/index.php/t-333284.html
However this MSDN article suggests that "member templates can also be defined outside of a class." http://msdn.microsoft.com/en-us/library/swta9c6e(v=vs.80).aspx
Is there something I am missing here?
template <class type>
class BinaryTree{
private:
Node<type>* root;
void internalTraverse(Node<type>* n){}
......
public:
......
};
template <class type>
void BinaryTree<type>::internalTraverse(Node<type>* n){
if (n == NULL){
return;
}
internalTraverse(n->left); // <- This declaration has no storage type or identifier
cout << "Node: " << n->data << endl;// Ditto
internalTraverse(n->right); // Ditto
};
......
For full disclosure: This is part of a homework assignment. However, its not directly related to the purpose of the assignment -- more of a tangential problem
Upvotes: 1
Views: 1557
Reputation: 477030
You defined the member function both inline and out-of-line, so that's multiple. The following compiles:
#include <iostream>
template <typename> class Node;
template <class type>
class BinaryTree{
private:
Node<type>* root;
void internalTraverse(Node<type>* n); // <--- no definition!
};
template <class type>
void BinaryTree<type>::internalTraverse(Node<type>* n){
if (n == NULL){
return;
}
internalTraverse(n->left);identifier
std::cout << "Node: " << n->data << std::endl;
internalTraverse(n->right);
}
Upvotes: 3