Thread: Trying to overload << in a templated Binary Tree

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Sep 2008
    Posts
    33

    Trying to overload << in a templated Binary Tree

    So that if I have a binary tree:

    Code:
    BinaryTree<int> *t = new BinaryTree<int>(5);
    And there have been more numbers inserted I can just say:

    Code:
    cout << bn;
    I have overloaded this operator before, but never with templates, so I am unsure where I should start.

    Here is my BinaryTree class:

    Code:
    template <class T>
    class BinaryTree{
    	public:
    		BinaryTree(T); //constructor
    		BinaryTree<T>* insert(T, BinaryTree<T>*);
    		void inorder(BinaryTree<T>*); //prints using inorder traversal
    		int countNodes(BinaryTree<T>*);
    	private:
    		T data;
    		BinaryTree* left;
    		BinaryTree* right;
    };
    I believe the function should be something like this:
    Code:
    friend ostream& operator<< <>(ostream&, const BinaryTree<T>&)
    I know you can't put << or >> overloaded functions as a member function, so does this not require a prototype, or just put it outside of the class and above the implementation?

    Hope this makes sense, thanks.

    *EDIT* Actually, I am supposed to make it a friend function, so I would put it in the class
    Last edited by chinesepirate; 11-23-2008 at 11:28 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 0
    Last Post: 11-04-2006, 11:07 AM
  2. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  3. Tutorial review
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 03-22-2004, 09:40 PM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  5. BST/Red and Black Tree
    By ghettoman in forum C++ Programming
    Replies: 0
    Last Post: 10-24-2001, 10:45 PM