1

I have two classes, the first that contains a vector and a get-method like this:

std::vector<mytype> const& MyClassA::getvector(std::vector<mytype>::iterator &it)
{
    it = this->myiterator;

    return this->myvector;
}

std::vector<mytype> myvector;
std::vector<mytype>::iterator myiterator;

and I have another class where I should use the vector:

MyClassB::myfunction()
{
    std::vector<mytype>::iterator it;
    std::vector<mytype> vector = MyClassA->getvector(it);

    if (it > vector.begin())
    {
        ....
        (--it)->dosomestuff();
        ....
    }
}

in the if I get a runtime exception: iterators incompatible. Why?

EDIT 1

void MyClassA::setvector(mytype myelement)
{
    this->myvector.push_back(myelement);
    this->myiterator = this->myvector.end() - 1;
}
2
  • Where are you initializing myiterator? Commented Jun 23, 2011 at 14:44
  • @Rocky added one more function to the answer Commented Jun 23, 2011 at 14:48

2 Answers 2

10

You have made a copy of the vector in the calling code, and the iterator isn't valid because it's still pointing to the original vector.

You probably intended something like this:

const std::vector<mytype> & vector = MyClassA->getvector(it);
Sign up to request clarification or add additional context in comments.

11 Comments

@Mark it works but I have still a problem, I should move the iterator in the MyClassB function and save the current position in the original variable stored in class MyClassA
@Stefano Switch the parameter and the result. Store a copy in the parameter and return the new iterator. newvec.begin()+(olditer-oldvec.begin()) should work.
@Let I need to get a vector as return from the function. It all works now but it doesn't save in the original iterator variable the new value, MyFunctionB is working on an iterator copy
@Stefano Well, then take int as a parametr and store the distance there.
@Let, good solution, anyway just for curiosity why isn't the function passing the original iterator rather than a copy?
|
1

Since you return std::vector<mytype> _const_&, the type of its iterator is std::vector::const_iterator.

EDIT:

Ah yes, I'm wrong and Mark Ransom is right. I leave this answer here because of the discussion in the comments, which itself may be useful to someone else.

7 Comments

@sehe You can't get an iterator out of constant vector, but this is totally irrelevant in this case.
@sehe: @vines is correct, from a const vector&, only const member functions will be called, which return const_iterator (not const iterator).
@Ben But he doesn't have a const vector.
@Let_Me_Be: He has a const vector&, the return type of getvector().
@Ben: that's irrelevant; the code compares an iterator to a const_iterator, which is valid (or would be if they came from the same vector). The problem has nothing to do with what is const and what isn't.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.