Thread: Specifying generics with nested classes

  1. #1
    Registered User
    Join Date
    May 2010
    Posts
    178

    Specifying generics with nested classes

    I am using a public outer class and a private inner class:

    Code:
    class List {	// public outer class
    
    	class Node {	// private inner class
    
    		/*
    		 * private instance variables
    		 */
    
    		string dbName;
    		Node *link;	// reference
    
    	public:
    
    		....
    	};
    
    	Node *head;	// private instance variable
    
    public:
    
    	List(){
    		head = NULL;
    	}
    
    	List::Node *addToStart(string new_dbName){
    		head = new Node(new_dbName, head);
    
    		return head;
    	}
    
    	.....
    };
    I would like to define a generic template

    Code:
    template <class T>
    class List {
       ....
    }
    With a single class or function, template definition and use I understand. However, nested class use is generating compiler errors related to

    Code:
    List::Node *addToStart(string new_dbName){
             head = new Node(new_dbName, head);
    
    	 return head;
    }
    Error specifies C++ ANSI forbids Node not having a type. I do not understand this error as Node is a type itself.

  2. #2
    Registered User
    Join Date
    Aug 2011
    Location
    Montreal, Quebec, Canada
    Posts
    73
    Code:
    List<T>::Node *addToStart(string new_dbName){
             head = new Node(new_dbName, head);
    
    	 return head;
    }

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Imanuel
    However, nested class use is generating compiler errors related to
    Are you defining this as a member function within the class template definition? If so, just changing List::Node to Node should work. If you are implementing this member function outside of the class template definition, then I would expect something like:
    Code:
    template<typename T>
    typename List<T>::Node* List<T>::addToStart(string new_dbName){
        head = new Node(new_dbName, head);
    
        return head;
    }
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    178
    I defined the function within the outer class and not as a prototype so I will use Node as return type and it should compile fine.
    Last edited by Imanuel; 09-30-2011 at 01:09 PM.

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You cannot refer to anything within a template class with unknown compile-time parameters (such as unknown types, T, etc) without specifying that it is a type.
    Hence, inside the class, Node is fine.
    Outside the class, it must be typename List<T>::Node, where T is an unknown type (ie a template parameter or the like). If T is known, then typename is not required.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Registered User
    Join Date
    May 2010
    Posts
    178
    You guys are so smart; a veritable living repository of knowledge!

    Before the transition from

    Code:
    List::Node
    to

    Code:
    Node
    I had more errors than lines of code ... C++ is an awesome language.

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    But it is also very tricky, especially when it comes to templates. It requires a lot of experience to do it right. Keep that in mind.
    Also, this might be of help in the future, [35] Templates Updated! , C++ FAQ
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    Registered User
    Join Date
    May 2010
    Posts
    178
    Thank you for all the replies.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Nested classes
    By VirtualAce in forum C++ Programming
    Replies: 4
    Last Post: 09-05-2007, 07:55 AM
  2. nested classes
    By vaibhav in forum C++ Programming
    Replies: 8
    Last Post: 09-27-2005, 07:51 AM
  3. Nested Classes
    By manofsteel972 in forum C++ Programming
    Replies: 4
    Last Post: 11-21-2004, 11:57 AM
  4. nested classes
    By jimothygu in forum C++ Programming
    Replies: 2
    Last Post: 04-09-2003, 11:39 AM
  5. nested classes...
    By doubleanti in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 12-09-2001, 04:25 PM