2011-07-04 4 views
1

可変長オブジェクトの1つがすべてのユニットのベクトルとして格納されているコードで作業しています。つまり、各文字が1単位であり、同じ文字が同じ高レベルオブジェクトです。 ベクターは、以下のようなものを含んでいてもよい...標準ベクトルconst_iterator代入アクセス違反

このベクトルは[]例えば

- obj[0] returns an const_iterator to the first "a" 
- obj[1] to the first "b" 
- obj[2] to the first "c" etc. 

const AI::GeneArray::const_iterator& AI::Chromosome::operator[](int index) const { 
    GeneArray::const_iterator pGene; 
    int cIndex; 
    for (cIndex=0,pGene = this->vGenes.cbegin();pGene != this->vGenes.cend();pGene++, cIndex++) { 
     if (index == cIndex) { break; } 
     (*pGene)->EndOfBlock(pGene); } 
    return pGene; } 

上記ベクターのため、その中に定義された演算子を持つクラスの内部プライベート変数aaaabbcccdeeffffあります次の機能があります。

AI::GeneArray::const_iterator function = vChromosome[0]; 

これにより、アクセス違反エラーが発生します。 FINAL CALLが私のデバッガ(のVisual C++ 2010 Expressの)によると

_Iterator_base12& operator=(const _Iterator_base12& _Right) 
    { // assign an iterator 
     if (_Myproxy != _Right._Myproxy) 
      _Adopt(_Right._Myproxy->_Mycont);<- This line 
     return (*this); 
    } 

にある

AI.exe!std::_Vector_const_iterator<std::_Vector_val<AI::Gene *,std::allocator<AI::Gene *> > >::_Vector_const_iterator<std::_Vector_val<AI::Gene *,std::allocator<AI::Gene *> > >(const std::_Vector_const_iterator<std::_Vector_val<AI::Gene *,std::allocator<AI::Gene *> > > & __that) + 0x2f byte 
AI.exe!std::_Iterator012<std::random_access_iterator_tag,AI::Gene *,int,AI::Gene * const *,AI::Gene * const &,std::_Iterator_base12>::_Iterator012<std::random_access_iterator_tag,AI::Gene *,int,AI::Gene * const *,AI::Gene * const &,std::_Iterator_base12>(const std::_Iterator012<std::random_access_iterator_tag,AI::Gene *,int,AI::Gene * const *,AI::Gene * const &,std::_Iterator_base12> & __that) + 0x2f bytes 
AI.exe!std::_Iterator_base12::_Iterator_base12(const std::_Iterator_base12 & _Right) Line 118 
AI.exe!std::_Iterator_base12::operator=(const std::_Iterator_base12 & _Right) Line 123 + 0x5 bytes 

を次のようにコールスタックの私の機能上記

_Right._Myproxy-> 
    _Mycont = CXX0030: Error: expression cannot be evaluated 
    _Myfirstiter = CXX0030: Error: expression cannot be evaluated 
エルス

私のプロジェクトでは、正しく動作するベクトルではなく、std :: listを使用して同様のコードを持っています

parents = new ChromosomeList::const_iterator[C]; 
*(parents) = --(this->vChromosomes.cend()); 
for (int i=1;i<C;i++) { 
    *(parents+i) = ChromosomeList::const_iterator((*(parents+i-1))); 
    (*(parents+i))--; } 

私は[0]

この値が正しいタイプのものであり、

const AI::GeneArray::const_iterator & 

私は同様の問題をGoogleで検索しました

vChromosomeによって返された値をチェックしました私が見つけられたのは、イテレータのベクトルを使ってループする問題です。

すなわち

for (AI::GeneArray::const_iterator pGene = Genes.cbegin();pGene != Genes.cend();pGene++) 

私のプロジェクトのこのようなコードは正しく動作しています。

答えて

1
AI::GeneArray::const_iterator function = vChromosome[0]; 

コンパイルしないでください。 functionはイテレータですが、値の内容の値に設定しようとしています。代わりにvChromosome.begin()が欲しくないのですか?

これはちょうどタイプミスであると仮定すると、あなたのバグは割り当てが発生した時点ではなく、あなたのバグはその前のどこかにあります。例えば、が空の場合、operator[](0)にアクセスしようとすると、未定義の動作につながる可能性があります。 (ベクトルが最初であっても有効であるかどうかを確認してください!)

(ちなみに、

parents = new ChromosomeList::const_iterator[C]; 

は、なぜあなたは手動でこのような配列を管理している?何vectorするためのものであることはありませんか?:) )

+0

vChromosomeはベクターではなく、大きなオブジェクトを構成するサブオブジェクト(遺伝子)のベクターを含むそのクラスであり、シーケンスに配置されたときにはサブオブジェクトが次に来るもの 私は、このクラスの[]演算子を定義して、遺伝子のベクトルに格納されているn番目の大きなオブジェクトの最初の遺伝子にcont_iteratorを返します。 ベクトルが有効で、イテレータがvChromosome [0]によって返された場合でも、私はこれらの値を見るためにデバッガを使いました。 – glenflet

+0

@glenflet:イテレータが有効な場合、そのイテレータを割り当てても、何も無効化されたり、アクセス違反が発生することはありません。他のクラスの 'operator []'のように、あなたが私たちに示していないコードのどこかで未定義の振る舞いをしているように聞こえます。 –

+0

問題が解決しました。私はoperator []関数でローカル変数を返していました。 – glenflet

関連する問題