Thread: problem with templates

  1. #1
    Registered User
    Join Date
    Apr 2006
    Posts
    2

    problem with templates

    hello, i am hoping that someone knowledgable about C++ templates could help solve a problem i am having. Before i explain the problem, i want to show a simplified version of the code I've got for the class that I'm having trouble with:

    Code:
    template <typename TYPE>
    class linked_list
    {
    private:
    	template <typename TYPE>
    	class node
    	{
    		TYPE * data;
    	};
    
    public:
    	node<TYPE> & operator[](unsigned int index);
    };
    The problem is that when I try to make a declaration for the operator[]() method, I get many errors. I want it to return a reference to a linkedlist::node class. I attempted to declare it like this:

    Code:
    template <typename TYPE>
    linked_list<TYPE>::node<TYPE> & linked_list<TYPE>::operator[](unsigned int index)
    {
    }
    I would think that something like this would work. Microsoft Visual C++ 8.0 says that the node type that's in the linked_list class is equivilent to

    Code:
    class linked_list<TYPE>::node<typename TYPE>
    I would be appreciative if someone would be able to tell me the proper way to make my declaration for operator[](). Thank you.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You might have to use typename, e.g.
    Code:
    template <typename TYPE>
    typename linked_list<TYPE>::node<TYPE> & linked_list<TYPE>::operator[](unsigned int index)
    {
    }
    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

  3. #3
    Registered User
    Join Date
    Apr 2006
    Posts
    2
    Thank you, that did the trick.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 06:24 PM
  2. Questions about Templates
    By Shamino in forum C++ Programming
    Replies: 4
    Last Post: 12-18-2005, 12:22 AM
  3. Replies: 5
    Last Post: 11-07-2005, 11:34 PM
  4. Problem with templates
    By Lazy Student in forum C++ Programming
    Replies: 3
    Last Post: 11-17-2002, 12:57 PM