How to use iterators within a template

Hi, I'm working on a vector class that is basically supposed to work the same way as the actual vector class in the c++ library. I have started to make the outline of the code but am having some issues understand the syntax of templates and iterators. Can someone help me? I am also pretty sure that my typedef declarations in the beginning are wrong. Any help on those would also be appreciated.

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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
#include <string>
#include <iterator>
#pragma once

using namespace std;


template <class T>
class Vector
{
public:
	typedef size_t size_type;
	typedef T& reference;
	typedef const reference const_reference;
	typedef T value_type;
	class Vector<T>::iterator InputIterator;
	Vector();
	Vector(size_type n);
	Vector(const Vector& x);
	Vector& operator=(const Vector& x);
	iterator begin();
	iterator end();
	size_type size() const;
	void resize(size_type n);
	size_type capacity() const;
	bool empty() const;
	void reserve(size_type n);
	reference operator[](size_type n);
	const_reference operator[](size_type n) const;
	reference at(size_type n);
	const_reference at(size_type n) const;
	reference front();
	const_reference front() const;
	reference back();
	const_reference back() const;
	value_type* data();
	const value_type* data() const;
	void push_back(const value_type& val);
	void pop_back();
	void clear();
	template<class InputIterator> Vector(InputIterator first, InputIterator last);
	~Vector();

private:
	T arr[];
	int usedSize;
	int actualSize;
};

template <class T>
Vector<T>::Vector()
{
	arr = new T[0];
}

template <class T>
Vector<T>::~Vector()
{
	delete arr;
	delete usedSize;
	delete actualSize;
}

template <class T>
Vector<T>::Vector(size_type n)
{
	arr = new T[n];
	usedSize = n;
	actualSize = n;
}

template<class T>
Vector<T>::Vector(const Vector<T> & x)
{
	arr = new T[x.size()]
	for (int i = 0; i < x.size(); i++)
	{
		arr[i] = *x.at(i);
	}
	usedSize = x.usedSize;

}

template<class T>
Vector<T> & Vector<T>::operator=(const Vector & x)
{
	// TODO: insert return statement here
}

template<class T>
iterator Vector<T>::begin()
{
	return iterator();
}

template<class T>
iterator Vector<T>::end()
{
	return iterator();
}

template<class T>
Vector<T>::size_type Vector<T>::size() const
{
	return nullptr;
}

template<class T>
void Vector<T>::resize(size_type n)
{
}

template<class T>
Vector<T>::size_type Vector<T>::capacity() const
{
	return size_type();
}

template<class T>
bool Vector<T>::empty() const
{
	return false;
}

template<class T>
void Vector<T>::reserve(size_type n)
{
}

template<class T>
Vector<T>::reference Vector<T>::operator[](size_type n)
{
	return reference();
}

template<class T>
Vector<T>::const_reference Vector<T>::operator[](size_type n) const
{
	return *this;
}

template<class T>
Vector<T>::reference Vector<T>::at(size_type n)
{
	return &T[n];
}

template<class T>
Vector<T>::const_reference Vector<T>::at(size_type n) const
{
	return const_reference();
}

template<class T>
Vector<T>::reference Vector<T>::front()
{
	return reference();
}

template<class T>
Vector<T>::const_reference Vector<T>::front() const
{
	return const_reference();
}

template<class T>
Vector<T>::reference Vector<T>::back()
{
	return *this;
}

template<class T>
Vector<T>::const_reference Vector<T>::back() const
{
	return *this;
}

template<class T>
Vector<T>::value_type* Vector<T>::data()
{
	return nullptr;
}

template<class T>
const value_type * Vector<T>::data() const
{
	return nullptr;
}

template<class T>
void Vector<T>::push_back(const value_type & val)
{
	return nullptr;
}

template<class T>
void Vector<T>::pop_back()
{
	return nullptr;
}

template<class T>
void Vector<T>::clear()
{
	return nullptr;
}


I'm getting a 'std::iterator': use of class template requires template argument list error for my begin() function as well as several 'T':undeclared identifier errors on several functions.
Last edited on
I strongly suggest implementing and testing incrementally. First, implement a constructor. Then, test it and move on to another function. Forget "making an outline of the code."

Iterator:
typedef std::iterator<std::random_access_iterator_tag, T> iterator;

Line 45 is illegal.

We don't delete things we haven't allocated with new.

If we allocated something with new[], we must use delete[].
Topic archived. No new replies allowed.