首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >标准向量const_iterator赋值访问冲突

标准向量const_iterator赋值访问冲突
EN

Stack Overflow用户
提问于 2011-07-04 10:20:40
回答 1查看 1.8K关注 0票数 1

我正在编写的代码中,更多可变长度对象中的1个被存储为所有单元的向量,即假设每个字母是一个单元,并且相同的字母是相同的高级对象。向量可能包含如下内容: aaaabbcccdeeffff...

此向量是一个类的内部私有变量,该类定义了一个operator[],使得对于上面的向量

代码语言:javascript
复制
 - 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; }

然后,在另一个函数中,我有以下内容

代码语言:javascript
复制
AI::GeneArray::const_iterator function = vChromosome[0];

这会导致访问冲突错误。

我的函数上面的调用堆栈如下

代码语言:javascript
复制
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

最后一个调用是

代码语言:javascript
复制
_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学习版)

代码语言:javascript
复制
_Right._Myproxy->
   _Mycont = CXX0030: Error: expression cannot be evaluated
   _Myfirstiter = CXX0030: Error: expression cannot be evaluated

否则在我的项目中,我有类似的代码使用std::list,而不是正常工作的vector

代码语言:javascript
复制
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

该值的类型正确,

代码语言:javascript
复制
const AI::GeneArray::const_iterator &

我在谷歌上搜索过类似的问题,我所能找到的就是与使用迭代器遍历向量有关的问题

代码语言:javascript
复制
for (AI::GeneArray::const_iterator pGene = Genes.cbegin();pGene != Genes.cend();pGene++)

这样的代码在我的项目中工作正常。

EN

回答 1

Stack Overflow用户

发布于 2011-07-04 12:29:21

代码语言:javascript
复制
AI::GeneArray::const_iterator function = vChromosome[0];

不应编译。function是一个迭代器,但您试图将其设置为值的内容的值。你确定你不想要vChromosome.begin()吗?

假设这只是一个打字错误,你的bug不会在赋值发生的地方,你的bug应该在那之前的某个地方。例如,vChromosome可能为空,在这种情况下,尝试访问operator[](0)将导致未定义的行为。(首先看看向量是否有效!)

(附注:

代码语言:javascript
复制
parents = new ChromosomeList::const_iterator[C];

为什么要手动管理这样的数组?这不就是vector的作用吗?:)

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6566785

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档