Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/haskell/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ std向量常量迭代器分配访问冲突_C++_Iterator_Variable Assignment_Runtime Error_Stdvector - Fatal编程技术网

C++ std向量常量迭代器分配访问冲突

C++ std向量常量迭代器分配访问冲突,c++,iterator,variable-assignment,runtime-error,stdvector,C++,Iterator,Variable Assignment,Runtime Error,Stdvector,我正在编写一个代码,其中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:

我正在编写一个代码,其中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];
这会导致访问冲突错误

my函数上方的调用堆栈如下所示

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
否则,在我的项目中,我有类似的代码使用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))--; }
我在谷歌上搜索过类似的问题,我所能找到的只是与使用迭代器循环遍历向量有关的问题

i、 e

我的项目中的这些代码工作正常

AI::GeneArray::const_iterator function = vChromosome[0];
不应该编译
function
是一个迭代器,但您试图将其设置为值内容的值。您确定不想改为
vChromosome.begin()

假设这只是一个输入错误,你的bug不会出现在任务发生的地方,你的bug会出现在之前的某个地方<例如,code>vChromosome可能为空,在这种情况下,尝试访问
运算符[](0)
将导致未定义的行为。(首先查看向量是否为偶数有效!)

(附注

parents = new ChromosomeList::const_iterator[C];
为什么要手动管理这样的数组?这不是
vector
的作用吗 )

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

假设这只是一个输入错误,你的bug不会出现在任务发生的地方,你的bug会出现在之前的某个地方<例如,code>vChromosome可能为空,在这种情况下,尝试访问
运算符[](0)
将导致未定义的行为。(首先查看向量是否为偶数有效!)

(附注

parents = new ChromosomeList::const_iterator[C];
为什么要手动管理这样的数组?这不是
vector
的作用吗
)

vChromosome,不是一个向量,它是一个包含子对象(基因)向量的类,这些子对象(基因)构成了大对象,当按顺序放置时,子对象在接下来的损害之前,这会创建块,我为这个类定义了一个the[]操作符,它返回并继续迭代到第n个大对象的第一个基因,储存在我的基因载体里。向量是有效的,也是vChromosome[0]返回的迭代器,我已使用我的调试器查看这些值。@glenflet:如果迭代器是有效的,对它进行赋值不会使任何内容无效或导致访问冲突。在我看来,在没有向我们展示的代码中(比如在另一个类的
操作符[]
中),似乎有未定义的行为。问题解决了,我在操作符[]函数中返回了一个局部变量。@glenflet::sign:--“单步执行”是您在调试器中的朋友。:)vChromosome,不是一个向量,它是一个包含子对象(基因)向量的类,这些子对象(基因)构成了大对象,当按顺序放置时,子对象先于下一个,这会创建块,我为这个类定义了一个the[]操作符,它返回并继续迭代器,到第n个大对象的第一个基因,储存在我的基因载体里。向量是有效的,也是vChromosome[0]返回的迭代器,我已使用我的调试器查看这些值。@glenflet:如果迭代器是有效的,对它进行赋值不会使任何内容无效或导致访问冲突。在我看来,在没有向我们展示的代码中(比如在另一个类的
操作符[]
中),似乎有未定义的行为。问题解决了,我在操作符[]函数中返回了一个局部变量。@glenflet::sign:--“单步执行”是您在调试器中的朋友。:)
for (AI::GeneArray::const_iterator pGene = Genes.cbegin();pGene != Genes.cend();pGene++)
AI::GeneArray::const_iterator function = vChromosome[0];
parents = new ChromosomeList::const_iterator[C];