[英]vector iterator not dereferencable at runtime on a vector<vector<vector<A*>*>*>
I have this destructor that create error at runtime "vector iterator not dereferencable". 我有这个析构函数,它在运行时创建错误“矢量迭代器不可解引用”。
The gridMatrix is a std::vector<std::vector<std::vector<AtomsCell< Atom<T> * > * > * > * > gridMatrix是std :: vector <std :: vector <std :: vector <AtomsCell <Atom <T> *> *> *> *>
I added the typename and also the typedef but I still have the error. 我添加了类型名和typedef,但仍然出现错误。
I will move for this idea of vect of vect* of vect* to use boost::multi_array I think, but still I want to understand were this is wrong. 我将移动vect *的vect *的vect的想法,以使用我认为的boost :: multi_array,但是我仍然想了解这是错误的。
/// @brief destructor
~AtomsGrid(void) {
// free all the memory for all the pointers inside gridMatrix (all except the Atom<T>* )
//typedef typename ::value_type value_type;
typedef std::vector<AtomsCell< Atom<T>* >*> std_vectorOfAtomsCell;
typedef std::vector<std_vectorOfAtomsCell*> std_vectorOfVectorOfAtomsCell;
std_vectorOfAtomsCell* vectorOfAtomsCell;
std_vectorOfVectorOfAtomsCell* vectorOfVecOfAtomsCell;
typename std_vectorOfVectorOfAtomsCell::iterator itSecond;
typename std_vectorOfVectorOfAtomsCell::reverse_iterator reverseItSecond;
typename std::vector<std_vectorOfVectorOfAtomsCell*>::iterator itFirst;
//typename std::vector<AtomsCell< Atom<T>* >*>* vectorOfAtomsCell;
//typename std::vector<std::vector<AtomsCell< Atom<T>* >*>*>* vectorOfVecOfAtomsCell;
//typename std::vector<std::vector<AtomsCell< Atom<T>* >*>*>::iterator itSecond;
//typename std::vector<std::vector<AtomsCell< Atom<T>* >*>*>::reverse_iterator reverseItSecond;
//typename std::vector<std::vector<std::vector<AtomsCell< Atom<T>* >*>*>*>::iterator itFirst;
for (itFirst = gridMatrix.begin(); itFirst != gridMatrix.end(); ++itFirst) {
vectorOfVecOfAtomsCell = (*itFirst);
while (!vectorOfVecOfAtomsCell->empty()) {
reverseItSecond = vectorOfVecOfAtomsCell->rbegin();
itSecond = vectorOfVecOfAtomsCell->rbegin().base();
vectorOfAtomsCell = (*itSecond); // ERROR during run: "vector iterator not dereferencable"
// I think the ERROR is because I need some typedef typename or template ???!!!
// the error seems here event at itFirst
//fr_Myit_Utils::vectorElementDeleter(*vectorOfAtomsCell);
//vectorOfVecOfAtomsCell->pop_back();
}
}
fr_Myit_Utils::vectorElementDeleter(gridMatrix);
}
If someone want the full code that create the error I'm happy to give it but I do not think we can attach file in the forum. 如果有人想要产生错误的完整代码,我很乐意提供,但是我认为我们不能在论坛中附加文件。 BUT still its is not very big so if you want it I can copy past it here.
但是仍然不是很大,所以如果您愿意,我可以在这里复制过去。
Thanks 谢谢
If v
is a std::vector
then v.rbegin().base() == v.end()
is true. 如果
v
是std::vector
则v.rbegin().base() == v.end()
为true。 In your code, itSecond
is actually equal to vectorOfVecOfAtomsCell.end()
, which is beyond the vector end and not dereferencable. 在您的代码中,
itSecond
实际上等于vectorOfVecOfAtomsCell.end()
,它超出了向量的范围并且不可取消引用。 See the MSDN page for reverse_iterator::base
for more information. 有关更多信息,请参见MSDN页面上的
reverse_iterator::base
。
As to solutions: in the code you posted, I don't really see why you need itSecond
at all. 关于解决方案:在您发布的代码中,我真的看不到为什么需要
itSecond
。 vectorOfAtomsCell = *reverseItSecond
should yield the result you are expecting, without the error. vectorOfAtomsCell = *reverseItSecond
应该产生预期的结果,而不会出现错误。 If you really need an iterator to the last element you should either do itSecond = vectorOfVecOfAtomsCell->end()-1
or itSecond = reverseItSecond.base()-1
. 如果确实需要最后一个元素的迭代器,则应该执行
itSecond = vectorOfVecOfAtomsCell->end()-1
或itSecond = reverseItSecond.base()-1
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.