I’m new to using Templates in C++, and I’m trying to get iterate through a Template list.
Here is my code
template <typename T>
void pleaseWork(const list<T>& aList, list<T>& list1, list<T>& list2)
{
typename list<T>::iterator i;
int n = 0;
for(i = aList.begin(); i != aList.end(); ++i) {
//Do something crazy
n++;
}
}
int main()
{
list<int> lista;
list<int> list1;
list<int> list2;
for (int i = 0; i < 10; i++) {
lista.push_back(i*2);
}
pleaseWork(lista, list1, list2);
return 0;
}
When I compile I get the following error:
error: no match for ‘operator=’ in i = ((const std::list<int, std::allocator<int> >*)aList)->std::list<_Tp, _Alloc>::begin with _Tp = int, _Alloc = std::allocator’
Thanks for your help
aListis a reference-to-const, sobegin()will return aconst_iterator.Use
autoif your compiler supports it.