Hey guys, new poster here. I'm rather desperate here, I just do not understand the concept of templates very well. I've used the search function, but I don't see the problem addressed in any other threads. Any help is much appreciated.
My assignment is to create a templated vector class using the following prototypes/definitions:
//I cannot modify these prototypes at all.
#ifndef _MYVECTOR
#define _MYVECTOR
#include <iostream>
using namespace std;
class BADINDEX{};
template <class T>
class containerInterface
{
public:
virtual containerInterface <T>& pushFront(T) = 0;
virtual containerInterface <T>& pushBack(T) = 0;
virtual containerInterface <T>& popFront(T&) throw(BADINDEX) = 0;
virtual containerInterface <T>& popBack(T&) throw(BADINDEX) = 0;
virtual int getSize() = 0;
virtual bool isFull() = 0;
virtual bool isEmpty() = 0;
virtual T front() throw(BADINDEX) = 0;
virtual T back() throw(BADINDEX) = 0;
virtual T& operator[] (int) throw(BADINDEX) = 0;
virtual void erase() throw(BADINDEX) = 0;
};
template <class T>
class myVector:public containerInterface<T>
{
public:
myVector();
~myVector();
myVector(const myVector&);
myVector <T>& operator=(myVector&);
myVector <T>& pushFront(T);
myVector <T>& pushBack (T);
myVector <T>& popFront (T&) throw(BADINDEX);
myVector <T>& popBack (T&) throw(BADINDEX);
T front() throw(BADINDEX);
T back() throw(BADINDEX);
T& operator[] (int) throw(BADINDEX);
int getSize();
bool isFull();
bool isEmpty();
void erase();
private:
T *data;
int size;
int capacity;
void grow();
void shiftRight();
void shiftLeft();
};
I think I understand how to do it, and have written all the functions, but it will not compile, and I think my constructor, copy constructor, and destructor are the primary culprits, since they do not have a bracketed <T> as part of the function header. Here are the functions I've written:
myVector::myVector()
{
size = 0;
capacity = 10;
data = new T [capacity];
}
myVector::~myVector()
{
delete [] data;
}
myVector::myVector(const myVector& other)
{
size = other.size;
capacity = other.capacity;
data = new T [capacity];
for(int i = 0; i < size; i++)
{
data[i] = other.data[i];
}
}
myVector<T>& myVector::operator=(myVector& other)
{
if(data != other.data)
{
delete[] data;
size = other.size;
capacity = other.capacity;
data = new T [capacity];
for(int i = 0; i < size; i++)
{
data[i] = other.data[i];
}
}
return *this;
}
myVector<T>& myVector::pushFront(T n)
{
shiftRight();
data[0] = n;
return *this;
}
myVector<T>& myVector::pushBack(T n)
{
size++;
grow();
data[size] = n;
return *this;
}
myVector<T>& myVector::popFront(T& n)
{
n = data[0];
shiftLeft();
return *this;
}
myVector<T>& myVector::popBack(T& n)
{
n = data[size];
size--;
return *this;
}
T front()
{
return data[0];
}
T back()
{
return data[size];
}
T& operator[](int n)
{
return data[n];
}
int getSize()
{
return size;
}
bool isFull()
{
return (size == capacity);
}
bool isEmpty()
{
bool answer = false;
if(size == 0)
{
answer = true;
}
return answer;
}
void erase()
{
delete []data;
size = 0;
capacity = 10;
data = new T [capacity];
}
void grow()
{
while(size >= capacity)
{
T* temp;
temp = data;
capacity *= 2;
data = new T[capacity];
for(int i = 0; i < size; i++)
{
data[i] = temp.data[i];
}
delete []temp;
}
}
void shiftRight()
{
size++;
grow();
for(int i = size; i > 0; i--)
{
data[i] = data[i - 1];
}
}
void shiftLeft()
{
size--;
for(int i = 0; i < size; i++)
{
data[i] = data[i + 1];
}
}
I get a compile error on the line of the constructor telling me that I can't use the template class myVector without template parameters. This makes sense, but I don't know how to fix it. Any help is appreciated. Thanks.