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

「それ」の 1 つ (もう 1 つの 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

それはあたかもそうであるように見えますthat(constおそらく const 参照)。ではなく をbegin() const返します。const_iteratoriterator

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 に答える