【发布时间】:2011-07-04 02:20:39
【问题描述】:
我正在编写代码,其中 1 个可变长度对象存储为所有单位的向量,即假设每个字母是一个单位,并且相同的字母是相同的高级对象。 该向量可能包含以下内容:aaaabbcccdeeffff...
这个向量是一个类的内部私有变量,它定义了一个操作符[],这样对于上面的向量
- 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; }
然后在另一个函数中我有以下内容
AI::GeneArray::const_iterator function = vChromosome[0];
这会导致访问冲突错误。
我的函数上面的调用栈如下
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
最后的电话是
_Iterator_base12& operator=(const _Iterator_base12& _Right)
{ // assign an iterator
if (_Myproxy != _Right._Myproxy)
_Adopt(_Right._Myproxy->_Mycont);<- This line
return (*this);
}
根据我的调试器(Visual C++ 2010 Express)
_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))--; }
我已经检查了返回的值
vChromosome[0]
这个值的类型正确,
const AI::GeneArray::const_iterator &
我在 Google 上搜索过类似的问题,我只能找到与使用迭代器循环遍历向量相关的问题
即
for (AI::GeneArray::const_iterator pGene = Genes.cbegin();pGene != Genes.cend();pGene++)
我项目中的此类代码运行正常。
【问题讨论】:
标签: c++ iterator variable-assignment runtime-error stdvector