0

抱歉,问题标题含糊不清,但我在这里有这些 typedef:

typedef std::list<AnimationKeyframe> Timeline;
typedef Timeline::iterator Keyframe;

让我们说一个这样的类:

class foo
{
Timeline timeline;
Keyframe left,right;
};

我还有一个复制/赋值构造函数,因为我正在存储迭代器(这似乎是个坏主意),而问题就在那里。

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

一旦我尝试使用“那个”(另一个 foo)之一分配关键帧,我就会收到看起来像模棱两可问题的错误。

二进制 '=' :未找到采用 '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>>
 ]

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

4

1 回答 1

6

看起来好像thatconst(可能是 const 引用)。begin() const返回一个const_iterator,而不是一个iterator

没有从 const_iterator 到 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
}
于 2011-04-21T14:12:01.283 回答