Assistance in my assignment.

Hello,
My instructor has given as a vector.h file, and she added "@todo" in them, where as we have to add or change things were ever it has a "///@todo" therefore, my question is that I have done them, however I want to see if i have done them right. If anyone can assist me, that will be great. I didn't finish the last "@todo" because I have never used "vector clear()" before.

Vector.h file source 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
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
  template<typename T>
class vector {
  public:

    ////////////////////////////////////////////////////////////////////////////
    /// @name Types
    /// @{

    typedef T* iterator;             ///< Random access iterator
    typedef const T* const_iterator; ///< Const random access iterator

    /// @}
    ////////////////////////////////////////////////////////////////////////////

    ////////////////////////////////////////////////////////////////////////////
    /// @name Constructors
    /// @{

    /// @brief Default constructor
    /// @param n Size
    /// @param val Initial value
    vector(size_t n = 0, const T& val = T()) : cap(std::max(size_t(10), 2*n)), sz(n) {
      /// @todo Implement default construction
	  
      t = new T[cap];
	  for(unsigned int i = 0; i < sz; ++i)
	  {
		  t[i] = val;
	  }
    }
    /// @brief Copy constructor
    /// @param v
    vector(const vector& v) : cap(v.cap), sz(v.sz) {
      /// @todo Implement copy construction
      t = new T[cap];
	  for(unsigned int i = 0; i < sz; ++i)
	  {
		  t[i] = val;
	  }
    }
    /// @brief Destructor
    ~vector() {
      /// @todo Implement destruction
	  delete[i] = val;
    }

    /// @brief Copy assignment
    /// @param v
    /// @return Reference to self
    vector& operator=(const vector& v) {
      /// @todo Implement copy assignment
      val = v;
	  
	  return *this;
    }

    /// @}
    ////////////////////////////////////////////////////////////////////////////

    ////////////////////////////////////////////////////////////////////////////
    /// @name Iterators
    /// @{

    /// @return Iterator to beginning
    iterator begin() {return t;}
    /// @return Iterator to beginning
    const_iterator cbegin() const {return t;}
    /// @return Iterator to end
    iterator end() {return &t[sz];}
    /// @return Iterator to end
    const_iterator cend() const {return &t[sz];}

    /// @}
    ////////////////////////////////////////////////////////////////////////////

    ////////////////////////////////////////////////////////////////////////////
    /// @name Capacity
    /// @{

    /// @return Size of vector
    size_t size() const {return sz;}
    /// @return Capacity of underlying array
    size_t capacity() const {return cap;}
    /// @return Does the vector contain anything?
    bool empty() const {return sz == 0;}

    /// @brief Resize the array
    /// @param n Size
    /// @param val Value if size is greater of default elements
    ///
    /// If the new size is less than the current progressively remove elements
    /// in the array. Otherwise progressively insert values at the end equal to
    /// \c val.
    void resize(size_t n, const T& val = T()) {
      /// @todo Implement resize (hint: use push_back and pop_back)
	  v.push_back(n);
	  v.pop_back(n);
    }
    /// @brief Request a change in the capacity
    /// @param c Capacity
    ///
    /// If the capacity is equal or less than the current capacity, nothing
    /// happens. If the capacity is greater, then the vector is reallocated and
    /// and contents copied.
    void reserve(size_t c) {
      /// @todo Implement reserve (hint: this function can be reused elsewhere)
	  size_t newc;
	  newc = c;
	  T*j = new T[cap];
	  if(newc <= cap)
	  {
		  return;
	  }
	  else
	  {
		  for(size_t i = 0; i < sz; i++)
		  {
			  j[i] = val;
		  }
	  }
	  delete[] t;
	  cap = c;
	  t = j;
    }

    /// @}
    ////////////////////////////////////////////////////////////////////////////

    ////////////////////////////////////////////////////////////////////////////
    /// @name Element Access
    /// @{

    /// @brief Element access without range check
    /// @param i Index
    /// @return Element at \c i
    T& operator[](size_t i) {return t[i];}
    /// @brief Element access without range check
    /// @param i Index
    /// @return Element at \c i
    const T& operator[](size_t i) const {return t[i];}
    /// @brief Element access with range check
    /// @param i Index
    /// @return Element at \c i
    T& at(size_t i) {
      if(i < 0 || i >= sz)
        throw std::out_of_range("Invalid Array Access");
      return t[i];
    }
    /// @brief Element access with range check
    /// @param i Index
    /// @return Element at \c i
    const T& at(size_t i) const {
      if(i < 0 || i >= sz)
        throw std::out_of_range("Invalid Array Access");
      return t[i];
    }
    /// @return Element at front of vector
    T& front() {return t[0];}
    /// @return Element at front of vector
    const T& front() const {return t[0];}
    /// @return Element at back of vector
    T& back() {return t[sz-1];}
    /// @return Element at back of vector
    const T& back() const {return t[sz-1];}

    /// @}
    ////////////////////////////////////////////////////////////////////////////

    ////////////////////////////////////////////////////////////////////////////
    /// @name Modifiers
    /// @{

    /// @brief Add element to end of vector, when capacity is reached perform
    ///        doubling strategy.
    /// @param val Element
    void push_back(const T& val) {
      /// @todo Implement push back
	  if (sz+1 > maxsize)
		  allo_new(2*c);
	  array[sz]=val;
	  sz++;
    }
    /// @brief Add element to end of vector, when capacity is reached perform
    ///        incremental strategy.
    /// @param val Element
    void push_back_incremental(const T& val) {
      /// @todo Implement push back with incremental capacity increase
	  if (sz+1 > c)
		  allo_new();
	  array[sz]=val;
	  sz++;
    }
    /// @brief Remove the last element of the vector
    void pop_back() {
      /// @todo Implement pop back
	  for(int i = 0; i < sz; i++)
	  {
		  t[i] = val;
	  }
    }
    /// @brief Insert element before specified position
    /// @param i Position
    /// @param val Value
    /// @return Position of new value
    iterator insert(iterator i, const T& val) {
      /// @bonus Implement insert for bonus
      return iterator();
    }
    /// @brief Remove element at specified position
    /// @param i Position
    /// @return Position of new location of element which was after eliminated
    ///         one
    iterator erase(iterator i) {
      /// @bonus Implement erase for bonus
      return iterator();
    }
    /// @brief Removes all elements without resizing the capacity of the array
    void clear() {
      /// @todo Implement clear
	  
    }

    /// @}
    ////////////////////////////////////////////////////////////////////////////

  private:
    T* t;       ///< Dynamically allocated array
    size_t cap; ///< Capacity
    size_t sz;  ///< Size
};

}
Copy constructor is incorrect. The copy constructor must copy each value from the parameter val and put it in t. Right now you assign each element of t to the vector parameter val, which won't compile. (Hint: t[i] should get a copy of val[i]).

Destructor is incorrect. Go back and look at the proper syntax for deleting dynamic arrays.

Copy assignment is incorrect. First, check that an object isn't being assigned to itself (&v != this). If it isn't, delete the current contents of the internal array, similar to the destructor. Then, allocate enough space to copy over the elements inside the parameter vector.

I haven't looked at the rest.
Thank you,
It's confusing to me who is a beginner at coding.
I'll try and fix them.
Okay, let me take a look- bit of warning, I have not touched full C++ in a while but it should all still be good info:

1) Your copy constructor (line 33) gets the value for cap and sz directly from passed object v despite the fact that those variables are private; it should use the appropriated getters. Also, line 38 is wrong; what's the contents of val here? Does val even exist?

2) Your destructor is... well, it's incorrect. What's i? What's val? They're not variables in the object, they're not passed values, so where do they come from?

3) Your copy assignment is also incorrect. I'm actually rather certain that because of the code for this, your copy constructor, and destructor, this code won't even compile. Again, what's val? You have the right idea with the copy constructor; the copy assignment just does the same thing except with an object that already exists instead of creating one.

4) Line 94, your resize function, is wrong. You call push_back(n) which is passing to push_back the size that you want the vector to be, except push_back is expecting to be given something to stick onto the internal array and will treat it as such. You also call pop_back(n) except the function definition for pop_back given to you doesn't take variables. Also, this function should it even work wouldn't even do anything to the size of the object.

5) Line 107 and 108 are entirely redundant. The rest of your reserve function, suffers from problems of order (why would you make the array that you intend to copy all the values to before deleting the original before checking whether it's even necessary to do so? That's a memory leak) and logic (Why would you make the new internal array the same size of the old array if you intend on making the array bigger?)

6) Line 179 calls a function allo_new(int), but that function is never defined. Do you mean reserve(size_t)? Also, you check of sz+1 is greater than maxsize on line 178, but maxsize isn't defined anywhere either. Do you mean cap?

7) Same problem with push_back_incremental as there is with push_back in regards to using allo_new(), but you managed to get the if statement correct.

8) Your pop_back does absolutely nothing and makes no sense; val doesn't exist, why are you iterating through an array when you're trying to delete the last element, etc.

I mean, just take a look here: http://www.cplusplus.com/reference/vector/vector/
@Ispil

First of, thank you for taking your time to help me.
I expected quite a lot of errors from the code.
I will go through and change things up, and see if it compiles.
My assignment is do tomorrow, but for some one who hasn't coded before, it hurts seeing others being able to understand and get the stuff fairly quick, while I process the info pretty slow.
All our instructor does is talk about it, and they won't show it to us, I mean I am a visual learner, but eh.
Thanks again.
Topic archived. No new replies allowed.