我正在编写的代码中,更多可变长度对象中的1个被存储为所有单元的向量,即假设每个字母是一个单元,并且相同的字母是相同的高级对象。向量可能包含如下内容: aaaabbcccdeeffff...
此向量是一个类的内部私有变量,该类定义了一个operator[],使得对于上面的向量
- 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学习版)
_Right._Myproxy->
_Mycont = CXX0030: Error: expression cannot be evaluated
_Myfirstiter = CXX0030: Error: expression cannot be evaluated否则在我的项目中,我有类似的代码使用std::list,而不是正常工作的vector
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
该值的类型正确,
const AI::GeneArray::const_iterator &我在谷歌上搜索过类似的问题,我所能找到的就是与使用迭代器遍历向量有关的问题
即
for (AI::GeneArray::const_iterator pGene = Genes.cbegin();pGene != Genes.cend();pGene++)这样的代码在我的项目中工作正常。
发布于 2011-07-04 12:29:21
AI::GeneArray::const_iterator function = vChromosome[0];不应编译。function是一个迭代器,但您试图将其设置为值的内容的值。你确定你不想要vChromosome.begin()吗?
假设这只是一个打字错误,你的bug不会在赋值发生的地方,你的bug应该在那之前的某个地方。例如,vChromosome可能为空,在这种情况下,尝试访问operator[](0)将导致未定义的行为。(首先看看向量是否有效!)
(附注:
parents = new ChromosomeList::const_iterator[C];为什么要手动管理这样的数组?这不就是vector的作用吗?:)
https://stackoverflow.com/questions/6566785
复制相似问题