I am creating a template class in C++, and am using std::list in it. For some reason, my compiler doesn't like how I'm trying to declare and construct an iterator for the list.
In my HashTable.h file I have:
template <typename T> class HashTable {
bool find(T thing) {
// some code
list<T>::iterator iter;
for (iter = table[index].begin(); iter != table[index].end(); ++iter) {
// some more code
}
}
}
And it gives me HashTable.h:77: error: expected ';' before "iter" as an error message.
What's the proper syntax for the iterator?
Or is that right, and I need to create an iterator class for each of the classes I intend to use in my HashTable template? If so, that would suck...
You need to use typename to tell the compiler that list<T>::iterator is indeed a type in this context.
typename list<T>::iterator iter;
The reasons are fairly obscure; see the C++ FAQ for more details: http://www.parashift.com/c++-faq-lite/templates.html#faq-35.18.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With