Trouble with list iterator

This is a discussion on Trouble with list iterator within the C++ Programming forums, part of the General Programming Boards category; I'm trying to run through a std::list to print some elements see if they hace been correctly inserted in my ...

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    8

    Trouble with list iterator

    I'm trying to run through a std::list to print some elements see if they hace been correctly inserted in my Hash Table but I just can't understand why the following declaration is wrong (Compiler keeps on telling me my iterator is not defined or asks me for a semicolon right before its name declaration) PLEASE HELP!

    Code:
    /**
     *
     *
     *
     *
     **/
    template <class T> class hash{
    	public:
    	
    	hash(int n){
    		this->numEntradas = n;
    		this->table = new vector<list<T> >();
    		this->table->resize(numEntradas);
    	}
    	
    	~hash(){ 
    		delete this->table;
    		this->table = 0;
    		this->numEntradas = 0; 
    	}
    	
    	T* search(const T& item){
    	
    	}
    	
    	void insert(const T& item){
    		int indice = hashFunction(item);
    		this->table->at(indice).push_front(item);
    	}
    	
    	
    	
    	int hashFunction(const T& key){
    		return (key % numEntradas);
    	}
    	
    	void printHashTable(){
    		for(int i = 0; i < numEntradas; ++i){
    			list<T> * buffer = this->table[i];
    			list<T>::iterator posicion = buffer->begin();
    			
    	        }
    	}

  2. #2
    Registered User
    Join Date
    Aug 2005
    Posts
    266
    if your error is anything like this, its because you need a semicolon after your class braces

    Quote Originally Posted by g++
    d.cpp:1: error: new types may not be defined in a return type
    d.cpp:1: note: (perhaps a semicolon is missing after the definition of ‘myclass’)
    d.cpp:5: error: two or more data types in declaration of ‘main’

  3. #3
    Registered User
    Join Date
    Oct 2010
    Posts
    8
    Didn't work, thanks anyways.... any other idea?

  4. #4
    Registered User
    Join Date
    Oct 2010
    Posts
    8
    Please Help Someone!!

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    list<T>::iterator usually requires the typename keyword in front of it:
    Code:
    typename list<T>::iterator posicion = buffer->begin();

  6. #6
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,587
    You have severe problems with the class from what I see. Drop the new all the time. You don't need them!
    Plus I don't see any members in the class.
    You can also drop the "this->" part.
    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.

  7. #7
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,304
    Oh yeah never declare a pointer to a vector as a class member. Doing so is always wrong.
    Here's a fixed version of your constructor:
    Code:
    	hash(int n) : numEntra(n) , table(n) {}
    Far shorter and more efficient.
    Oh, and here's how the destructor of that class should look:
    Code:
    //
    That's right, poof it's gone, uneeded, unnecessary etc.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. One more linked list implementation
    By BlackOps in forum C Programming
    Replies: 17
    Last Post: 07-16-2009, 09:34 PM
  2. linked list iterator class inheritience
    By neandrake in forum C++ Programming
    Replies: 2
    Last Post: 11-14-2004, 09:03 AM
  3. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 09:33 PM
  4. Search and Build Tree
    By 1999grandamse in forum C++ Programming
    Replies: 17
    Last Post: 11-14-2002, 12:36 PM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM

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