Std::vector iterator incompatibles?

Your getA() function returns a vector by value. You're initializing your loop iterator to the beginning of that vector, but since the returned vector is temporary, it is destroyed at the end of that line at the end of this line the vector returned by getA is gone, so it_A is invalid. Std::vector::const_iterator it_A = it_B->getA().begin() Therefore the iterator is no longer valid.

You should instead return a reference to the vector like this (note the & ): const std::vector & getA() const {return myAs;}.

Your getA() function returns a vector by value. You're initializing your loop iterator to the beginning of that vector, but since the returned vector is temporary, it is destroyed at the end of that line. // at the end of this line the vector returned by getA is gone, so it_A is invalid.

Std::vector::const_iterator it_A = it_B->getA().begin(); Therefore the iterator is no longer valid. You should instead return a reference to the vector like this (note the &): const std::vector & getA() const {return myAs;}.

That was... kind of obvious. Thanks a lot JoshD! – Xavier V.

Oct 22 '10 at 13:02.

You realize that B::getAs() returns a copy of its myAs. So the vector that it_A ranges over is a different copy than the one whose getA().end() it keeps being compared to... I.e. The for loop never stops.(Even worse, it_A ranges over a vector that is a temporary, so it's destroyed and all kinds of random crap could be written over that storage while you're pretending to iterate through it.).

Thanks for the complete and simple repro. The problem here is that you're using iterators from 2 different vectors, this is a runtime debug check. You probably didn't intend to do this, but it is a result of the return type of getA.

You're returning a copy of the vector and your probably meant to return a reference to the vector, like this: const std::vector& getA() const {return myAs;}.

I have an error (vector iterator incompatibles) during execution in my C++ program that I do not understand. Did I missed something? Thanks in advance for your answers.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions