dynamic template array classs

Hello everyone I am working on a exercise for a class of mine. I have been looking at this for too long I need a fresh set of eyes. In my dynamic_template_array class the addEntry() function works fine if I add pass one entry to it. If I send more that one entry to it in a loop it does nothing wont print out anything. I have tried a multitude of things well you'll see.If someone could just let me know where I went wrong I would really appreciate it. Thanks.

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

#include <string.h>

template<class T>
class DynamicTemplateArray
{

public:
	DynamicTemplateArray( );
	DynamicTemplateArray(int size);
	DynamicTemplateArray(const DynamicTemplateArray& other);
	~DynamicTemplateArray();
	DynamicTemplateArray& operator=(const DynamicTemplateArray& other);
	int findIndexToRemove(T& item2Remove);
	int getSize( ) const;
	void addEntry(const T& entry);
	bool deleteEntry(T entry2Remove);
	T& getEntry(int index) const;

private:
	int _size;
	T *_dynamicArray;

};

template<class T>
DynamicTemplateArray<T>::DynamicTemplateArray(		):
							_size(0),
							_dynamicArray(NULL)
{  }

template<class T>
DynamicTemplateArray<T>::DynamicTemplateArray(		int size):
							_size(size)
{ _dynamicArray = new T[_size]; }

template<class T>
DynamicTemplateArray<T>::DynamicTemplateArray(const DynamicTemplateArray<T> &other)  //<-------------TESTED
{
	this->_size = other._size;
	this->_dynamicArray = new T[_size];
	
	for(int i =0; i < _size; i++)
		this->_dynamicArray[i] = other._dynamicArray[i];
}

template<class T>
DynamicTemplateArray<T>::~DynamicTemplateArray()	//<-------- TESTED
{
	delete [] _dynamicArray;
}

template<class T>
DynamicTemplateArray<T>& DynamicTemplateArray<T>::operator=(const DynamicTemplateArray<T>& other)  //<-------- TESTED
{
	this->_size = other._size;
	this->_dynamicArray = new T[_size];
	memcpy(this->_dynamicArray, other._dynamicArray, sizeof(_dynamicArray));
	return this;
}

template<class T>
int DynamicTemplateArray<T>::getSize( ) const
{
	return _size;
}

template<class T>
void DynamicTemplateArray<T>::addEntry(const T& entry)  //<-------- WORKS WITH SINGLE ENTRY WONT WORK IN LOOP
{
	int newSize = _size +1;
	T* temp = new T[newSize];
	
//		for (int i = 0; i < _size; i++)
//		{
//			temp[i] = _dynamicArray[i]; 
//		}
//		
		memcpy(temp, _dynamicArray, sizeof(_dynamicArray));
		temp[_size] = entry;
                
//                 _dynamicArray = temp;                
//                temp = NULL;
                 
		std::swap(_dynamicArray, temp);
		_size++;
		delete [] temp;
		
}

template<class T>
bool DynamicTemplateArray<T>::deleteEntry(T entry2Remove)		//<-------- TESTED
{
	int index = findIndexToRemove(entry2Remove);
	
	if(index != -1)
	{
		T *temp = new T[_size-1];
			
		for (int i =0; i < index; i++)
		{
			temp[i] = _dynamicArray[i]; 
		}
		for (int i = index+1; i < _size; i++)
		{
			temp[i] = _dynamicArray[i]; 
		}
		std::swap(_dynamicArray, temp);
		_size--;
		delete [] temp;
		return true;
	}
	return false;
}

template<class T>
T& DynamicTemplateArray<T>::getEntry(int index) const	//<-------- TESTED
{
	return _dynamicArray[index];
}

template<class T>
int DynamicTemplateArray<T>::findIndexToRemove(T& entry2Remove)	//<-------- TESTED
{
	int index = -1;
	
	for (int i =0; i < _size; i++)
	{
		if (_dynamicArray[i] == entry2Remove)
		{
			index = i;
			break;
		}
	}
	return index;
}

#include <iostream>
#include "dynamic_template_array.h"



int main(int argc, char **argv)
{
	DynamicTemplateArray<int> intArray(5);
		
	//<------- START
	std::cout << "Array To Start" << std::endl;
	for(int i =0; i < intArray.getSize( ); i++)
		std::cout << intArray.getEntry(i) << std::endl;
	
	//<------- DELTING
	std::cout << "Deleting two elements...........\n" << std::endl;
	for(int i =0; i < intArray.getSize( ) -3 ; i++)
		intArray.deleteEntry(i);
	
	//<------- After DELETE
	std::cout << "Array After deleting" << std::endl;
	for(int i =0; i < intArray.getSize( ); i++)
		std::cout << intArray.getEntry(i) << std::endl;
	
	//<---------- ADDING
	std::cout << "Adding 2 elements................\n" << std::endl;
	for(int i =0; i < intArray.getSize( ); i++)
		intArray.addEntry(5);
	
	//<------- After ADD
	std::cout << "Array After Adding" << std::endl;
	for(int i =0; i < intArray.getSize( ); i++)
		std::cout << intArray.getEntry(i) << std::endl;

	return 0;
}
1
2
for(int i =0; i < intArray.getSize( ); i++)
		intArray.addEntry(5);


don't give a constant as a paramter to the function addEntry. It takes the "T" parameter as reference.
Thanks, I solved it on my own dumb mistake sorry for wasting your time I just need to get and walk away some times.
Last edited on
Just in case this turns up in a search and someone has a similar problem, the biggest issue was the use of sizeof on line 81. sizeof(pointer_type) yields the size of a pointer, and not size of the memory it points to.

Aside from that, one should never use memcpy in template code which is not restricted to trivial types. Instead, prefer std::copy:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <algorithm>

template<class T>
void DynamicTemplateArray<T>::addEntry(const T& entry)
{
    int newSize = _size +1;
    T* temp = new T[newSize];
	
    std::copy(_dynamicArray, _dynamicArray+_size, temp) ;
    temp[_size++] = entry;
                
    delete [] dynamicArray ;
    dynamicArray = temp ;
}


Last edited on
Thank you cire.
Topic archived. No new replies allowed.