Nested class error (Variable has incomplete type)

I wrote my vector and inside my class I also have iterator class.

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
template<typename T>
struct Vector {  

class Iterator;
  Vector &insert(Iterator it , T element)// HERE IS ERROR
{  
  arr_+it=element;
  return *this;
  }
private:
T* arr_=nullptr;
size_t size_;};
template <typename T>
class Vector<T>::Iterator
    : public std::iterator<std::random_access_iterator_tag, T> {
public:
  Iterator(T *ptr)
  :ptr_{ptr} 
  {}
 

  ~Iterator() {
  delete ptr_;
  }
private:
  T *ptr_;

};


I marked a spot where it gives me error. I gives me error only when I put Iterator type as ARGUMEN for function. I can put Iterator as return type, even if I put it as const Interator& it or just Iterator&it as function argument it doesn't give me error. Only if I put it as copy in funcion argument. And it says
Variable has incomplete type. I tried to copy that same code in Iterator class and it works fine, but my test.cpp needs it to be in Vector class


The compiler needs to know the full definition of what an Iterator is if you are passing it by value to the function, or using ("dereferencing") it.

Also, your indentation is a violation of the Geneva convention.
Last edited on
Could you help me, how could I solve it.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
template < typename T >
struct Vector {

    class Iterator ; // declaration

    Vector& insert( Iterator iter, T element ) ; // declaration
};

template < typename T >
class Vector<T>::Iterator /* : public etc. */ { // definition

    // ...
};

template < typename T >
Vector<T>& Vector<T>::insert( Vector<T>::Iterator iter, T element ) { // definition

   // use iter ...

   return *this ;
}
Topic archived. No new replies allowed.