operator != for iterators
-
Friends, I'm trying to create a function so I can print the conents of a STL list from main(). It looks like:
void TraverseAndDisplay(list< PhoneRecord>::iterator& itor, const list< PhoneRecord>& l) { while (itor != l.end()) { cout << *itor++ << " "; } cout << endl; }
Unfortunately, because PhoneRecord is not a primitive data type (it's my own class), the default operator != won't work. The exact error I'm getting is: error C2679: binary '!=' : no operator defined which takes a right-hand operand of type 'class std::list<class PhoneRecord,class std::allocator<class PhoneRecord> >::const_iterator' (or there is no acceptable conversion) Obviously I need to overload the != to work. I don't know how to overload the != operator to work for iterators in this case? Thanks in advance for your help! -
Friends, I'm trying to create a function so I can print the conents of a STL list from main(). It looks like:
void TraverseAndDisplay(list< PhoneRecord>::iterator& itor, const list< PhoneRecord>& l) { while (itor != l.end()) { cout << *itor++ << " "; } cout << endl; }
Unfortunately, because PhoneRecord is not a primitive data type (it's my own class), the default operator != won't work. The exact error I'm getting is: error C2679: binary '!=' : no operator defined which takes a right-hand operand of type 'class std::list<class PhoneRecord,class std::allocator<class PhoneRecord> >::const_iterator' (or there is no acceptable conversion) Obviously I need to overload the != to work. I don't know how to overload the != operator to work for iterators in this case? Thanks in advance for your help!Can you please repost the code, using < and > for the angle brackets so that we can see the template parameters? Dave http://www.cloudsofheaven.org
-
Can you please repost the code, using < and > for the angle brackets so that we can see the template parameters? Dave http://www.cloudsofheaven.org
-
Sorry about that. It's reposted. Ignore the space after the < and before PhoneRecord.. it was making it a smiley.
I suspect it is because you are comparing an iterator with a const_iterator. Since the list passed in as a parameter is constant, it will only return a const_iterator, so you could either make the list non-cost, or pass in a const_iterator instead of an iterator as the first parameter (the better option as you do not appear to be modifying the list here). Alternatively, you could pass in the end iterator as a parameter rather than passing in the entire list - this is the approach used by STL, as then the type of container can be changed without modifying the function itself. Dave http://www.cloudsofheaven.org
-
I suspect it is because you are comparing an iterator with a const_iterator. Since the list passed in as a parameter is constant, it will only return a const_iterator, so you could either make the list non-cost, or pass in a const_iterator instead of an iterator as the first parameter (the better option as you do not appear to be modifying the list here). Alternatively, you could pass in the end iterator as a parameter rather than passing in the entire list - this is the approach used by STL, as then the type of container can be changed without modifying the function itself. Dave http://www.cloudsofheaven.org