std::list Iterator doesn't get assigned
Sorry about the vague question title, but I have these typedefs here:
typedef std::list<AnimationKeyframe> Timeline;
typedef Timeline::iterator Keyframe;
and let's say a class like this:
class foo
{
Timeline timeline;
Keyframe left,right;
};
I also have a copy/assignment constructor because I'm storing iterators (a bad idea it seems), and the trouble lies just there.
Keyframe myTemp, hisTemp;
this->left = this->timeline.begin(); // assigns
myTemp = this->timeline.begin(); // assigns
hisTemp = that.timeline.begin() // does not assign
as soon as I am trying to assign a Keyframe with one of 'that' (the other foo) I get errors that look like an ambiguity problem.
binary '=' : no operator found which takes a right-hand operand of type 'std::_List_const_iterator<_Mylist>' (or there is no acceptable conversion)
Additional info:
with
[
_Mylist=std::_List_val<AnimationKeyframe,std::allocator<AnimationKeyframe>>
]
could be 'std::_List_iterator<_Mylist> &std::_List_iterator<_Mylist>::operator =(const std::_List_iterator<_Mylist> &)'
with
[
_Mylist=std::_List_val<AnimationKeyframe,std::allocator<AnimationKeyframe>>
]
while trying to match the argument list '(Keyfr开发者_如何学Pythoname, std::_List_const_iterator<_Mylist>)'
with
[
_Mylist=std::_List_val<AnimationKeyframe,std::allocator<AnimationKeyframe>>
]
What causes this odd behavior? I'm suspecting my bad style for using iterators as states...but I don't know how else I could do, aside from keeping pointers.
It looks as though that
is const
(perhaps a const reference). begin() const
returns a const_iterator
, not an iterator
.
The reason there is no conversion from const_iterator to iterator is as follows:
void foo(const std::vector<int> &vec) {
std::vector<int>::iterator it = vec.begin(); // if this compiled...
*it = 5; // then this would attempt to modify a vector taken by const ref
}
精彩评论