Skip to main content
GameDev.net gamedev.net
🔒 Locked

Iterator troubles

Started by l jsym l Dec 5, 2011 at 3:25 AM 1 replies 1k views
Original Post
l jsym l
l jsym l
Hey, Im creating an array of lists. I need to be able to iterate through the list for printing and comparison reasons. This code is not completely done by any means so the code I'll be posting is very rough. I'm just getting an error that is saying: "expression must have class type". Im getting this error where im creating my for loop for the iterator. If anyone has any suggestions let me know. Thanks!

#include "LinkingHash.h"



int LinkingHash::getHash( string str )
{
int result = 0;

for ( unsigned int i = 0; i < str.length(); i++ )
{
result = result + ( int( str ) );
}

return result % 13;
}

void LinkingHash::addToHash( string str )
{
hashIndex = getHash( str );

hashTable[hashIndex].push_back( str );
//cout << hashTable[hashIndex].back() << endl;

list<string>::const_iterator it;
for( it = hashTable.begin();
it != hashTable.end();
it++)
{

}

}

void LinkingHash::deleteFromHash( string str )
{
hashIndex = getHash( str );

vector<string> vec;

for( unsigned int i = 0; i < hashTable[hashIndex].size(); i++ )
{
string temp = hashTable[hashIndex].back();
hashTable[hashIndex].pop_back();

vec.push_back( temp );

if ( vec.back() == str )
{
vec.pop_back();
}
}

for( unsigned int i = 0; i < vec.size(); i++ )
{
string temp = vec.back();
vec.pop_back();

hashTable[hashIndex].push_back( temp );

}


}

void LinkingHash::printHashTable()
{
int index = 0;
vector<string> vec;

while( index < SIZE )
{
cout << index << ") ";

/*list<string>::const_iterator Iterator;
for(Iterator = hashTable.begin();
Iterator != hashTable.end();
Iterator++)*/


for( unsigned int i = 0; i < hashTable[index].size(); i++ )
{
string temp = hashTable[hashIndex].back();
hashTable[hashIndex].pop_back();

vec.push_back( temp );


}

for( unsigned int j = 0; j < vec.size(); j++ )
{
string temp = vec.back();
vec.pop_back();

cout << temp << " ";

hashTable[hashIndex].push_back( temp );

}

cout << endl;

index++;
}




}
l jsym l
fastcall22
fastcall22
Since your hash table is a [font="Courier New"]list>[/font], the iterator type should be [font="Courier New"]list>::const_iterator[/font]. If your compiler supports C++11, the type can be automatically deduced with the [font="Courier New"]auto[/font] keyword.
l jsym l
l jsym l
ah yes. Just figured it out, that was a big duh on my part.

Thanks
l jsym l

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.