template errors

This is a discussion on template errors within the C++ Programming forums, part of the General Programming Boards category; Code: template<class Type> class Tree { private: struct Node { Type item; // node[0] = parent; // node[1] = left ...

  1. #1
    -bleh-
    Join Date
    Aug 2010
    Location
    somewhere in this universe
    Posts
    458

    template errors

    Code:
    template<class Type>
    class Tree
    {
    private:
      struct Node
      {
        Type item;
        //  node[0] = parent;
        //  node[1] = left child;
        //  node[2] = right child;
        Node * node[3];
      };
      
      Node * root;
    public:
      Tree():root(NULL) {}
      Node* search_tree(Type x);
      
      
        
    };
    
    template<class Type>
    Tree<Type>::Node *  Tree<Type>::search_tree(Type x)
    {
      if( this == NULL) return NULL;
      if(this->item == x)
        return this;
      else if( this->item < x )
        return this->node[1].search_tree(x);
      else
        return this->node[2].search_tree(x);
    }
    That class template keeps giving me the errors;
    Code:
    error: expected constructor, destructor, or type conversion before * token
    does anyone know how to fix that error?
    "All that we see or seem
    Is but a dream within a dream." - Poe

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,528
    Code:
    template<class Type>
    typename Tree<Type>::Node *  Tree<Type>::search_tree(Type x)
    In rough terms, without the typename keyword present, the standard says that the compiler must interpret TreeType<Node> as a non-type name while parsing the template code. That is the reason for your error message.

    As to the reason the standard says that - view finding and understanding that as a homework challenge. The reason is not trivial, and a complete explanation gives most people a headache.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy in reply to you, it is likely you deserve it. Suck it up, sunshine, and read this, this, and this before posting again.

  3. #3
    -bleh-
    Join Date
    Aug 2010
    Location
    somewhere in this universe
    Posts
    458
    Thank you grumpy for the answer. I keep forgetting the use of "typename" for a user-defined type inside a class.
    "All that we see or seem
    Is but a dream within a dream." - Poe

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. template function argument deduce
    By George2 in forum C++ Programming
    Replies: 2
    Last Post: 03-11-2008, 09:56 PM
  2. Template overloading?
    By cpjust in forum C++ Programming
    Replies: 5
    Last Post: 02-20-2008, 03:21 PM
  3. instantiated from here: errors...
    By advocation in forum C++ Programming
    Replies: 5
    Last Post: 03-27-2005, 09:01 AM
  4. im extreamly new help
    By rigo305 in forum C++ Programming
    Replies: 27
    Last Post: 04-24-2004, 12:22 AM
  5. MSVC 6 SP5 template errors
    By jdinger in forum C++ Programming
    Replies: 2
    Last Post: 04-12-2003, 10:59 AM

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