【问题标题】:std vector const_iterator assignment Access violationstd 向量 const_iterator 赋值 访问冲突
【发布时间】: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


    【解决方案1】:
    AI::GeneArray::const_iterator function = vChromosome[0];
    

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

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

    (旁注,

    parents = new ChromosomeList::const_iterator[C];
    

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

    【讨论】:

    • vChromosome,不是一个向量,它是一个包含组成大对象的子对象(基因)向量的类,当放置在一个序列中时,之前的子对象会损害所发生的接下来,这将创建块,我为此类定义了一个 [] 运算符,以便它返回和 cont_iterator,到存储在我的基因向量中的第 n 个较大对象的第一个基因。向量是有效的,也是 vChromosome[0] 返回的迭代器,我已经使用我的调试器查看了这些值。
    • @glenflet:如果迭代器有效,则对其进行分配不会使任何内容无效或导致访问冲突。在我看来,您在未向我们展示的代码中某处有未定义的行为(例如在其他类的 operator[] 中)。
    • 问题解决了,我在 operator[] 函数中返回了一个局部变量。
    • @glenflet: :sigh: -- "Step Into" 是你在调试器中的朋友。 :)
    猜你喜欢
    • 1970-01-01
    • 2013-06-23
    • 2011-04-19
    • 1970-01-01
    • 1970-01-01
    • 2012-05-22
    • 1970-01-01
    • 2019-09-17
    • 2016-05-17
    相关资源
    最近更新 更多