Vector Implimentation Help

Hello everyone. I am currently on vacation at the moment and am quite a distance away from any computer with a compiler to be able to help test and debug my code. I was hoping that someone might be able to help me go about figuring out my header file while I am away? That would be great!

The assignment is to be able to implement a vector class with upper and lower bounds to allow the usage of negative indexes. I'm having trouble with making sure that my code is solid and solves the problem at hand. There is a need to keep track of where the zero-th element from the original vector would be in relation to the negative index(s).

I was wondering, if someone wouldn't mind telling me what is wrong/'could be better' with each method, that would be awesome! I really don't want to be GIVEN code but examples that touch on the subject would be acceptable too.

(**Trying to remain within the code of ethics and such so I am not expelled - primarily because I enjoy the challenge of programming and don't like being given the answer on a silver platter.)

If you could format the corrective criticism in this format**, that would be great!

Method Name('parameters'){

'''Put what you think needs/can be improved upon here'''
'''(No 'actual' code for this assignment should be placed here-only examples)'''

}

Any help with this would be wonderful. Thanks. :3

lwer_bnd_vector header file (.h)

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
#ifndef _LBVECTOR_H
#define _LBVECTOR_H

#include <cstdlib>
#include <cassert>
#include <iostream>

template <class Item> class lbvector
{
public:
   // default constructor
	lbvector( ) 
	{
		  myCapacity = 0;
		  Item* myList = new Item [];
		  myUpper = 0;
		  myLower = 0;
		  *myZero = nullptr;
	}
   
  // lbvector constructor given lower and upper bounds
   lbvector(int vLow, int vHigh) 
   {
   
	  myLower = vLow;
	  myUpper = vHigh;
	  const int size = 1 + (myUpper - myLower);
	  assert(size >= 0);
      Item* myList = new Item [size];
	  assert(myList != 0);
	  Item* myZero = myList - myLower;  
   }

   // lbvector constructor, given lower bound, upper bound and a fill value
   lbvector(int vLow, int vHigh, Item fillValue) 
   {
		myLower = vLow;
		myUpper = vHigh;
		const int size = 1 + (myUpper - myLower);
		assert(size >= 0);
		Item* myList = new Item [size];
		assert(myList != 0);
		fill(fillValue);
		Item* myZero = myList - myLower;
   }
   
   // copy constructor - makes a duplicate lbvector
       
   lbvector(const lbvector<Item> & vec) 
   {
      myUpper = vec.myUpper;
	  mylower = vec.myLower;
	  myCapacity = vec.myCapacity;
      myList* = new Item [myCapacity];
      assert(myList != 0);
        
      // copy elements from vec
      for (int k = 0; k < vec.myCapacity; k++) {
         myList[k] = vec.myList[k];
      }
	  Item* myZero = myList - myLower;
   }
    
   // destructs unneeded 'lower-bound' vector
   ~lbvector ( ) 
   {
      delete [] myList;
   }

   // assignment method
   //    need a deep copy when using dynamic memory
   // precondition: Item supports assignment     
   // postcondition: self is assigned vec
   lbvector & operator = (const lbvector<Item> & vec) 
   {
 
      if (this != &vec) {
         // out with old list, in with new
         delete [] myList;
         myCapacity = vec.myCapacity;
         myList = new Item [myCapacity];
         assert(myList != 0);
            
         // copy elements from vec
         for (int k=0; k < vec.myCapacity; k++) {
            myList[k] = vec.myList[k];
         }
      }
      return *this;           
   }

   //  resizes the applied lbvector to a new size, given a lower bound, upper bound
   //   and a desired new size
   void resize(int newLow, int newHigh, int newSize) 
   {
      assert(newSize >= 0);
      	  
      // allocate new storage
      Item * newList = new Item[newSize];
      assert(newList != 0);

      // copy retained values from old to new        
      for (int k=newLow; k < newHigh;k++) {
         newList[k] = myList[k];
      }

      // return space no longer needed        
      delete [] myList;

      // update instance variables to reflect changes
      myCapacity = newSize;
      myList = newList;
   }

   
   //retrieves the upper bound of the applied vector
   int upper( ) const
   {
		return myUpper;
   }
   
   //retrieves the lower bound of the applied vector 
   int lower( ) const
   {
		return myLower;   
   }

   //retrieves the capacity of the applied vector
   int capacity( ) const 
   {
      return myCapacity;
   }

   // fills a given vector with the given 'fillvalue'
   void fill(Item fillValue) 
   {
      for (int k=0; k < myCapacity; k++) {
         myList[k] = fillValue;
      }
   }

   // safe indexing, returning reference
   // precondition: 0 <= index < myCapacity
   // postcondition: return index-th item
   // exception: aborts if index is out-of-bounds
   Item & operator [] (int index) 
   {
      checkIndex(index);
      return myList[index];     
   }
    
   // const index 
   // safe indexing, returning const reference to avoid modification
   // precondition: 0 <= index < myCapacity
   // postcondition: return index-th item
   // exception: aborts if index is out-of-bounds
   const Item & operator [] (int index) const 
   {
      checkIndex(index);
      return myList[index]; 
   }

private:
	
	
	lbvector <Item> *  myList;  	// the array of items
	Item* myZero;  	//pointer to the element in the array that is considered to be the original '0'
	int myCapacity; // # things in vector (array), 0,1,...,(myCapacity-1)
	int myUpper;
	int myLower;
	
   
   // aborts with appropriate message if index is not in appropriate range
   // use std:: to access names in std namespace
   void checkIndex ( int index) const 
   {
      if (index < 0 || myCapacity <= index) {
         std::cerr << "Illegal vector index: " << index
                   << " (max = " << myCapacity-1 << ")" 
                   << std::endl;
         assert(index >= 0);
         assert(index < myCapacity);
      }
   }
};

#endif                // _LBVECTOR_H not defined 
Last edited on
You are leaking memory in your constructors and this *myZero = nullptr; is wrong.
@naraku9333: I think I understand the pointer issue (it should read 'int * myZero = nullptr;'), but your statement about 'leaking memory' is a bit broad. Could you be a bit more specific?
it should read 'int * myZero = nullptr;'
No that would declare an int pointer names myZero local to the function. You want myZero = nullptr;

Lines 29 and 41 Item* myList = new Item [size]; these are created local to the function and are not deleted so the memory is leaked.

Also, in resize() you have
1
2
3
Item * newList = new Item[newSize];
     ...
      myList = newList;
myList is an lbvector<Item>* but you are setting it to an Item*.
Topic archived. No new replies allowed.