Private Inner Classes

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • KiddoGuy
    New Member
    • Nov 2008
    • 17

    #1

    Private Inner Classes

    I'm trying to implement a linked list in C++ but I'm having a problem with an inner class.
    Code:
    template<class E>
    class LinkedList: public List {
    	private:
    		Node<E> head;
    		class Node<E> {
    			private:
    				E data;
    				Node* next;
    				Node* prev;
    			friend class List; 		
    		};
    	public:
    		//constructors and stuff here
    };
    I'm pretty sure I'm messing up generics somehow, and the head variable is giving me a problem
    Code:
    linkedlist.h:21: error: declaration of ‘class E’
    linkedlist.h:6: error:  shadows template parm ‘class E’
    linkedlist.h:21: error: ‘template<class E> class 'List’ used without template parameters
    Last edited by KiddoGuy; Dec 14 '08, 06:38 AM. Reason: formatting
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    Inside the template<class E> template the name E counts as a type name so you don't have to do anything like Node<E> on line four; a class 'Node' is enough.

    C++ uses a one pass compiler so you have to define the Node class before you can define a 'head' variable of that type.

    Are you sure that your want a Node head and not a Node* head in your List class? (this is not a compiler issue).

    kind regards,

    Jos

    Comment

    • KiddoGuy
      New Member
      • Nov 2008
      • 17

      #3
      Thank you, Jos. You've helped me many times.

      Hopefully my last problem before getting this to compile is this:
      Code:
      LinkedList<E>::Node<E> curNode;
      which gives "error: expected primary-expression before ‘>’ token".

      Since LinkedList is the enclosing class, this way of defining a Node called curNode makes sense to me but it is apparently wrong?

      P.S. Node is now a template as is LinkedList which extends a separate template

      Comment

      Working...