Using Template Class In Separate Class

This is a discussion on Using Template Class In Separate Class within the C++ Programming forums, part of the General Programming Boards category; I have a template class for a tree. Now I want to make a class called TreeIterator that will just ...

  1. #1
    Registered User
    Join Date
    Oct 2009
    Posts
    117

    Using Template Class In Separate Class

    I have a template class for a tree. Now I want to make a class called TreeIterator that will just iterate through the tree and return a node. Is there a way to include and declare a Tree variable without making TreeIterator also a template? Or would the best option just be to make the iterator a nested class? The code below is what I want to do -

    Code:
    #include "Tree.h"
    
    class TreeIterator {
    private:
    	Tree tree;
    };
    
    #endif
    But this is what compiles -

    Code:
    #ifndef TREEITERATOR_H
    #define TREEITERATOR_H
    #include "Tree.h"
    
    template<typename T>
    class TreeIterator {
    private:
    	Tree<T> tree;
    };
    
    #endif
    Any help is appreciated

  2. #2
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Why don't you want to make TreeIterator a template?
    If it iterates over Tree<T> then it should be a template too.
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  3. #3
    Registered User MacNilly's Avatar
    Join Date
    Oct 2005
    Posts
    324
    Code:
    #ifndef TREE_H
    #define TREE_H
    
    template<class T>
    struct TreeNode {
    private:
        int key;
        // .. etc
    };
    
    template<class T>
    class TreeIterator {
    public:
        TreeIterator(TreeNode<T>* nodePtr): node(nodePtr) { }
        int getKey() { return node->key; }
        bool hasNext() { // implement this }
        void next() { // implement this }
    
    private:
        TreeNode<T>* node; // reference instead of copy
    };
    
    template<typename T>
    class Tree {
    public:
        TreeIterator<T> getIterator()
        {
            return TreeIterator<T>(root);
        }
    private:
        TreeNode<T>* root;
    };
    
    #endif
    That's your basic setup for a container class with an iterator type (i.e., STL)

    The iterator holds a reference to a TreeNode since you don't want each iterator to make its own copy of a node and since you'll be dealing with Node pointers inside Tree. TreeNode is a struct and not part of the public interface.

    Returning straight up nodes is a bad idea. Just use TreeIterator as a proxy class for TreeNode.

    Check out the STL source code for a quality implementation.
    Last edited by MacNilly; 03-03-2011 at 04:44 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Singleton base template class? How do you do that?!
    By arcaine01 in forum C++ Programming
    Replies: 4
    Last Post: 07-13-2009, 04:12 AM
  2. template and friend class
    By black_spot1984 in forum C++ Programming
    Replies: 3
    Last Post: 10-21-2008, 05:50 PM
  3. deriving classes
    By l2u in forum C++ Programming
    Replies: 12
    Last Post: 01-15-2007, 04:01 PM
  4. Function template in Class template?
    By Aidman in forum C++ Programming
    Replies: 3
    Last Post: 10-28-2003, 08:50 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21