Reputation: 425
Hey I have a list that has values in it, I want to print it out using const &. I can do it with by just referencing it but when I put const in I get an error. I'm not sure why this is the case because the code won't edit the list. This is my code.
// Prints out the list declared
template<typename DATA_TYPE>
void print(const list<DATA_TYPE> &myList){
for(list<DATA_TYPE>::iterator pos = myList.begin(); pos != myList.end(); pos++){
DATA_TYPE currentWord = *pos;
cout << currentWord << " ";
}
}
If you could also go through the logic behind the error that'd be cool, Thanks.
Upvotes: 0
Views: 272
Reputation: 83537
Since your list
is declared as const
, you need to use a const_iterator
:
list<DATA_TYPE>::const_iterator pos
p.s. For future reference, please copy and paste any compiler errors that you get into your question.
Upvotes: 6