templates

This is a discussion on templates within the C++ Programming forums, part of the General Programming Boards category; When I try: Code: void Iterator<T>::nextBucket() { delete listIterator; while(tableIndex<tableSize && table[tableIndex]->GetSize()) tableIndex++; if(tableIndex<tableSize) listIterator=new ListIterator(table[tableIndex++]->GetFirst()); else listIterator=NULL; } in ...

  1. #1
    Registered User
    Join Date
    Dec 2006
    Posts
    40

    templates

    When I try:

    Code:
    void Iterator<T>::nextBucket()
    		{
    			delete listIterator;
    			while(tableIndex<tableSize && table[tableIndex]->GetSize())
    				tableIndex++;
    			if(tableIndex<tableSize)
    				listIterator=new ListIterator(table[tableIndex++]->GetFirst());
    			else
    				listIterator=NULL;
    		}
    in my .cpp file and this:

    Code:
    template <class T>
    class Iterator
    {
    	private:
    		int tableIndex;
    		int tableSize;
    		LinkedList<T>** table;
    		ListIterator<T>* listIterator;
    		
    		void nextBucket();
    	public:
    		Iterator(LinkedList<T>** const t, const int & s): tableSize(s), table(t), tableIndex(0), listIterator(NULL)
    		{}
    		
    		~Iterator();
    		
    		bool hasNext();
    		
    		const LLNode<T>* next();
    };
    in my .h file I get this error:

    src/HashSet.cpp:8: error: ‘T’ was not declared in this scope
    src/HashSet.cpp:8: error: template argument 1 is invalid
    and when I omit the <T> after Iterator in the cpp file I get the error:

    src/HashSet.cpp:8: error: ‘template<class T> class Iterator’ used without template parameters
    What am I doing wrong?

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Does
    Code:
    template <class T>
    void Iterator<T>::nextBucket()
    		{
    			delete listIterator;
    			while(tableIndex<tableSize && table[tableIndex]->GetSize())
    				tableIndex++;
    			if(tableIndex<tableSize)
    				listIterator=new ListIterator(table[tableIndex++]->GetFirst());
    			else
    				listIterator=NULL;
    		}
    work?

  3. #3
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,538
    Plus you should not put any templates in .cpp files.
    Keep it all in the headers.
    Btw, I prefer:
    template <typename T>
    Since using "class" in the template parameter list is pretty much a relic.

    http://www.parashift.com/c++-faq-lit...html#faq-35.12
    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.

  4. #4
    Registered User
    Join Date
    Dec 2006
    Posts
    40
    Okay, I'm back again. I moved everything over to the .h file and that made things a lot easier. I'm still using class, I will change that soon. But my next question is about the test() function. I can't add anything. I'm using:

    Code:
    void test()
    	{
    		cout<<"Starting Linked List Test"<<endl;
    		LinkedList<string> list;
    		list.Clear();
    		list.Insert(string("1st"), NULL);
           }
    the Insert looks like:

    Code:
    LLNode<T> * Insert(const T* v, LLNode<T> * n)
    	{
    		LLNode<T> *newNode = NULL;
    		if(n==NULL)
    		{
    			newNode= new LLNode<T>(v,NULL, LLRoot);
    			if(LLRoot==NULL)
    				LLRoot=newNode;
    			else
    				LLRoot->prev=newNode;
    				LLRoot=newNode;
    		}
    		else
    		{
    			newNode = new LLNode<T>(v,n,n->next);
    			if(n->next!=NULL)
    				n->next->prev=newNode;
    			n->next=newNode;
    		}
    		size++;
    		return newNode;
    	}
    And it gives me the error:

    inc/LinkedList.h:312: error: no matching function for call to ‘LinkedList<std::basic_string<char, std::char_traits<char>, std::allocator<char> > >::Insert(std::string&, NULL)’
    inc/LinkedList.h:226: note: candidates are: LLNode<T>* LinkedList<T>::Insert(const T*, LLNode<T>*) [with T = std::basic_string<char, std::char_traits<char>, std::allocator<char> >]
    I'm sorry about all the questions. I've never tried to do a templated class before! Thanks for the help everyone!

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Maybe cast NULL to be of the right type? (I'm not quite interested enough to write a compile-able example.)

  6. #6
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,659
    You could avoid the problem entirely by building your list class with sentinel nodes, so that as an argument n is never NULL. I'm way too lazy to make an example either ... In particular, sentinels are nodes which do not hold a useful value, but have links to the other parts of the list (e.g. next).

    In such a case it is a matter of trivia to make something like front() and to call Insert with its result.
    Code:
    LLNode<T> & front ( ) 
    {
      return !empty( ) ? *( LLRoot->next ) : *LLRoot;
    }
    
    list.Insert( something, &list.front( ) );
    Document that NULL is not an expected argument for Insert() and problem solved.
    Last edited by whiteflags; 11-01-2008 at 09:35 PM.

  7. #7
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Code:
    LLNode<T> * Insert(const T* v, LLNode<T> * n)
    Maybe it's because your Insert() function expects a pointer, but you're passing it an object?
    "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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Templates from DLL or static library problem
    By mikahell in forum C++ Programming
    Replies: 2
    Last Post: 01-01-2008, 12:49 AM
  2. Questions about Templates
    By Shamino in forum C++ Programming
    Replies: 4
    Last Post: 12-17-2005, 11:22 PM
  3. templates and inheritance problem
    By kuhnmi in forum C++ Programming
    Replies: 4
    Last Post: 06-14-2004, 02:46 AM
  4. When and when not to use templates
    By *ClownPimp* in forum C++ Programming
    Replies: 7
    Last Post: 07-20-2003, 09:36 AM

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