Skip to main content
gamedev.net
🔒 Locked

Iterators and Templates

Started by Mr Awesome Apr 12, 2006 at 1:42 AM 5 replies 1k views
Original Post
Mr Awesome
Mr Awesome
In my class template, I'm trying to pass an iterator to a function called isSelected, but it is not working at all. Here are the error messages: error: no matching function call to ComponentList::isSelected(std::_List_iterator&) note: candidates are: bool ComponentList::isSelected(typename std::list >::const_iterator&) const [with ComponentType = Vector2D] As far as I can tell, it's saying it can't cast from a std::_List_iterator& to a std::list::const_iterator&, which to me looks like just a cast from non-const to const. Any have any idea of what is wrong? Thanks in advance.
Extrarius
Extrarius
Which compiler are you using? I'm guessing that "_List_iterator" is an internal type since it starts with a reserved sequence (underscore followed by a capital letter), so the meaning will depend on your compiler.

Could you give the function prototype that you've created, including the template?
"Walk not the trodden path, for it has borne it's burden." -John, Flying Monk
Enigma
Enigma
Quote:
Original post by Mr Awesome
note: candidates are: bool ComponentList::isSelected(typename std::list >::const_iterator&) const [with ComponentType = Vector2D]

typename std::list_CharT> >::const_iterator&? Suspicious. That should almost certainly be typename std::listComponentType> >::const_iterator&.

Σnigma

Enigma
Enigma
Just had a closer look. It turns out the issue I highlighted above was a red herring. The real problem is const correctness. Since you are passing a non-const iterator to isSelected C++ will have to try and create a temporary const_iterator from it to pass to the function. However, the argument is taken by non-const reference and temporaries cannot be bound to non-const references. To solve the problem either declare the argument as a const reference or pass it by value.

Σnigma
Agony
Agony
If the above is not the solution, I also noticed that it appears that somewhere (definition/declaration?) your function might be declared as const

note: candidates are: bool ComponentList::isSelected(typename std::list >::const_iterator&) const [with ComponentType = Vector2D]

but somewhere else it is declare non-const

error: no matching function call to ComponentList::isSelected(std::_List_iterator&) //no const

That could just be the result of the error message stripping off the "const" however; I'm not sure that this is quite the error message that such an error would produce. But if nothing else works, might be worth a check.
"We should have a great fewer disputes in the world if words were taken for what they are, the signs of our ideas nly, and not for things themselves." - John Locke o
Mr Awesome
Mr Awesome
Thanks Enigma, the const reference solved it. I didn't know you couldn't create temporaries from a non-const reference. I guess you learn something every day.
RDragon1
RDragon1
Quote:
Original post by Mr Awesome
Thanks Enigma, the const reference solved it. I didn't know you couldn't create temporaries from a non-const reference. I guess you learn something every day.


If you could, it would lead to some very strange behavior. If it's a non-const reference, you're probably passing something to the function, with the intention that it will be modified (it's non-const, so it can be modified, and it's by reference, so it should modify the thing you passed in)... But if the object were created as a temporary, the compiler would silently create the temporary from the real object you passed in, modify the temporary, then destroy it - leaving the real object untouched - not what you intended.

Topic Locked

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

Sign in to reply to this topic.