Throwing exceptions
I am supposed to throw some exceptions in myVector class but when I
impliment them I get an error saying
myVector.h:139: error: looser throw specifier for `myVector&
myVector::popFront(T&) [with T = int]'
myVector.h:17: error: overriding `containerInterface&
containerInterface::popFront(T&) throw (BADINDEX) [with T = int]'
What is wrong with the way that I am throwing my exceptions in my
functions in the myVector class?
here is the .h
#ifndef _MYVECTOR
#define _MYVECTOR
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() = 0;
};
template <class T>
class myVector: public containerInterface<T>
{
private:
T *data;
int size;
int capacity;
void grow()
{
T *temp;
temp = data;
capacity *= 2;
data = new T[capacity];
for(int i = 0; i < size; i++)
{
data[i] = temp[i];
}
delete [] temp;
}
void shiftRight()
{
//decrement this iteration
for (int i = size; i >= 0; i--)
{
data[i] = data[i - 1];
}
size++;
}
void shiftLeft ()
{
for (int i = 0; i < size; i++)
{
data[i] = data[i + 1];
}
size--;
}
public:
myVector ()
{
size = 0;
capacity = 10;
data = new T[capacity];
}
~myVector ()
{
delete [] data;
}
myVector (const myVector& v)
{
size = v.size;
capacity = v.capacity;
data = new T[capacity];
for (int i = 0; i < size; i++)
{
data[i] = v.data[i];
}
}
myVector <T>& operator = (myVector& n)
{
delete [] data;
size = n.size;
capacity = n.capacity;
data = new T[capacity];
for (int i = 0; i < size; i++)
{
data[i] = n.data[i];
}
return *this;
}
myVector <T>& pushFront(T e)
{
if (size == capacity)
{
grow();
}
shiftRight();
data[0] = e;
return *this;
}
myVector <T>& pushBack (T e)
{
if (size == capacity)
{
grow();
}
data[size] = e;
size++;
return *this;
}
myVector <T>& popFront (T& n)
{
if(isEmpty())
{
throw (BADINDEX());
}
n = data[0];
shiftLeft();
return *this;
}
myVector <T>& popBack (T& n)
{
if(isEmpty())
{
throw (BADINDEX());
}
n = data[size - 1];
size--;
return *this;
}
T front()
{
if(isEmpty())
{
throw (BADINDEX());
}
return data[0];
}
T back ()
{
if(isEmpty())
{
throw (BADINDEX());
}
return data[size - 1];
}
T& operator [] (int n)
{
if ( n >= capacity || n < 0 )
{
throw (BADINDEX());
}
return data[n];
}
int getSize()
{
return size;
}
bool isFull()
{
bool status;
if (capacity == size)
{
status = true;
}
else
{
status = false;
}
return status;
}
bool isEmpty()
{
bool status;
if (size == 0)
{
status = true;
}
else
{
status = false;
}
return status;
}
void erase()
{
size = 0;
}
};
#endif