Michael Jasper
Michael Jasper

Reputation: 8058

External member functions of class template

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.

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

Answers (1)

Kerrek SB
Kerrek SB

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

Related Questions