Template Dynamic Array

I'm attempting to convert a dynamic array program into template form. I've got a lot of it to work but I keep running across the same errors. Any help I can get would be great.

Here is my h file:

#ifndef DYNARRAY_H
#define DYNARRAY_H

template<class T>
class DynArray {
public:
DynArray<T>::DynArray() {

sizeVal = 0;
capacityVal = defCapacity;
data = new T[capacityVal];

}

DynArray<T>::DynArray(T& n) {

data = new T; //This throws a syntax error for the = sign.
*data = *n.data;

}

DynArray<T>::DynArray(const DynArray & b) {

T temp = new T;
temp = b.temp;
copy(temp); //This throws an error that copy doesn't have a return type but the function is void.

}

DynArray::~DynArray() {

free();

}

void DynArray<T>::free() {

delete[] data;
}

const DynArray<T> & DynArray<T> ::operator=(const DynArray& b) {

if (this != &b) {

free();
copy(b);
}
return *this;

}

void DynArray<T>::copy(const DynArray& b) {

delete[] data;
data = b.data;

}

T& DynArray<T>::operator[](T n) const
{
if (n >= sizeVal) {

throw runtime_error("invalid index");
exit(0);

}
else {
return data[n];
}
}

int DynArray<T>::size() const {

return sizeVal;

}

int DynArray<T>::capacity() const {

return capacityVal;

}

void DynArray<T>::clear() {

sizeVal = 0;
delete[] data;
capacityVal = defCapacity;
data = new T[capacityVal];

}

void DynArray<T>::push_back(const T& n) {

if (capacityVal == sizeVal) {

grow();
*(data + sizeVal) = n;
sizeVal++;

}
else {

data[sizeVal] = n;
sizeVal++;

}
}

void DynArray<T>::pop_back() {

sizeVal--;

}

T& DynArray<T>::at(T m) const {

if (m >= sizeVal) {

throw runtime_error("invalid index");
return 0;

}

return *(data + m);

}

T& DynArray<T>::back() {

if (data == nullptr) {
throw runtime_error("Vector is currently empty");
}
else {

return data[sizeVal - 1];

}

}

T& DynArray<T>::front() {

if (data == nullptr) {
throw runtime_error("Vector is currently empty");
}
else {

return data[0];

}

}

private:
T* data = nullptr;
int capacityVal;
int sizeVal;
int defCapacity = 2;

void DynArray<T>::grow() {
cout << "grow" << endl;
int i = 0;

capacityVal = capacityVal * capacityVal;
T* temp = new T[capacityVal];

for (i = 0; i < sizeVal; i++) {

temp[i] = data[i];

}

delete[] data;
data = temp;

}

};

#endif
Also, if anyone knows how to put a trace in the grow function that would be greatly appreciated. I have no idea what they are talking about when they ask me to do it
Topic archived. No new replies allowed.