std :: list Iterator没有被分配

Eri*_*ius 0 c++ iterator stl ambiguity variable-assignment

对于模糊的问题标题感到抱歉,但我在这里有这些typedef:

typedef std::list<AnimationKeyframe> Timeline;
typedef Timeline::iterator Keyframe;
Run Code Online (Sandbox Code Playgroud)

让我们说一个这样的课:

class foo
{
Timeline timeline;
Keyframe left,right;
};
Run Code Online (Sandbox Code Playgroud)

我也有一个复制/赋值构造函数,因为我存储迭代器(看起来很糟糕),麻烦就在那里.

Keyframe myTemp, hisTemp;
this->left = this->timeline.begin(); // assigns
myTemp = this->timeline.begin(); // assigns
hisTemp = that.timeline.begin() // does not assign
Run Code Online (Sandbox Code Playgroud)

一旦我试图用一个'那个'(另一个foo)分配一个Keyframe,我就会得到看起来像模糊问题的错误.

binary'=':找不到哪个运算符采用'std :: _ List_const_iterator <_Mylist>'类型的右手操作数(或者没有可接受的转换)

附加信息:

  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 '(Keyframe, std::_List_const_iterator<_Mylist>)'
 with
 [
     _Mylist=std::_List_val<AnimationKeyframe,std::allocator<AnimationKeyframe>>
 ]
Run Code Online (Sandbox Code Playgroud)

是什么导致这种奇怪的行为 我怀疑使用迭代器作为状态的糟糕风格......但我不知道除了保持指针之外我还能做些什么.

Ste*_*sop 6

它看起来好像thatconst(也许是一个const引用).begin() const返回一个const_iterator,而不是一个iterator.

没有从const_iterator转换到迭代器的原因如下:

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
}
Run Code Online (Sandbox Code Playgroud)