"return" is not working fine (i'm trying to return an iterator)

Hi there,

first, I'm new in this community, so hello everyone (and sorry about my english, I'm from Spain and maybe I'm not speaking well)

Second, I am trying to return an iterator which I have created for a class, but the "return" (I think) is not working properly because the value of the iterator once is returned is random (I checked that before return it is OK)... I don't know why this is happening...

I'll post the code so you can see the whole code of the class and the iterator. I don't know where the error is, and I'm getting mad for 2 days.. The return which I'm talking about is in the function begin() of my class. Line 59 of this code.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
template <typename T>
class vectorDinamico {
    private:
        list<vector<T> > datos;
        int tamBloque;
    public:
        vectorDinamico();
        vectorDinamico ( int tam );
        ~vectorDinamico();
        vectorDinamico<T>& operator= ( const vectorDinamico<T> &v );
        vectorDinamico ( const vectorDinamico<T> &v );

        class iterator;
        friend class iterator;

        /* Clase iterator */
        class iterator {
            private:
                typename list<vector<T> >::iterator it_l;
                typename vector<T>::iterator it_v;
                friend class vectorDinamico;
            public:
                /* Implementamos algunos métodos del iterador */
                iterator();
                iterator ( const typename vectorDinamico<T>::iterator &it );
                iterator& operator++();
                iterator& operator--();
                T& operator*();
                iterator& operator= ( const typename vectorDinamico<T>::iterator &it );
            };
        /* Fin clase iterator */
        iterator begin();
        iterator end();
	

	template <typename U> friend ostream& operator<<(ostream& out,const vectorDinamico<U> &v);

        void insert ( iterator it, const T &x );
    };


template <typename T>
vectorDinamico<T>::iterator::iterator() {}

template <typename T>
vectorDinamico<T>::iterator::iterator ( const typename vectorDinamico<T>::iterator &it ) {
    it_l = it.it_l;
    it_v = it.it_v;
    }


template <typename T>
typename vectorDinamico<T>::iterator vectorDinamico<T>::begin() {
    typename vectorDinamico<T>::iterator it;
    vector<T> aux;
    it.it_l = datos.begin();
    aux = *it.it_l;
    it.it_v = aux.begin();
    return it;
    }

template <typename T>
typename vectorDinamico<T>::iterator& vectorDinamico<T>::iterator::operator= ( const typename vectorDinamico<T>::iterator &it ) {
    it_l = it.it_l;
    it_v = it.it_v;
    return *this;
    }

template <typename T>
T& vectorDinamico<T>::iterator::operator*() {
    return *it_v;
    }


If you need something else of this code, ask me for it, but I think that the problem has to be here.

Thanks a lot, it would be great if someone helps me!

Bests!
The aux vector is created inside the function and will not exist after the function has ended. That means that iterators like it.it_v that refers to that vector is no longer valid after the function has returned.
Oh my God..

Great, the problem is solved...

Thank you Peter87!!!

See you!
Topic archived. No new replies allowed.