weevilofdoom
Programmer
I've created a stack (LIFO), and on the "pop" function, it complains about incompatible types, yet it's templated, so there shouldn't be any issue. It is based off of a templated array class. It is boggling to say the least.
Here's a snip of my Stack class:
#include "template.h"
template <typename U, int capacity = 20>
class StackArray
{
public:
StackArray();
bool IsEmpty();
U& topEl();
U pop();
void push(U value);
private:
int getSize();
Array<U> *ra; <- my array
int size;
int top;
};
template <typename U, int capacity>
U StackArray<U>:
op()
{
try
{
if(top != -1)
{
//this line complained aboot:
return ra[top];
top--;
}
else
throw OutOfBounds("Hey, can't pop!"
;
}
catch(OutOfBounds obj)
{
obj.PrintErrorNo();
}
}
Error Produced by VS .NET:
error C2440: 'return' cannot convert from 'Array<T>' to 'int' with [ T = int ]
Here's my array class, sorry for the lengthy spam
:
template <typename T>
class Array
{
public:
Array();
Array(int length, int base = 0);
Array(const Array& copy );
virtual ~Array();
Array& operator=(const Array& anArray);
T& operator[](int index);
const T* getData() const;
int getBase() const;
int getLength() const;
void setBase(int newBase);
void setLength(int newLength);
protected:
T* m_data;
int m_base;
int m_length;
};
template <typename T>
Array<T>::Array() : m_data(0), m_length(0), m_base(0)
{ }
template <typename T>
Array<T>::Array(int length, int base = 0)
{
m_length = length;
if(m_length < 0)
throw OutOfBounds("Length was wrong size"
;
else
{
m_base = base;
m_data = new T[m_length];
memset(m_data, 0, m_length * sizeof(T));
}
}
template <typename T>
Array<T>::Array(const Array& copy)
{
m_length = copy.m_length;
if(m_length < 0)
throw OutOfBounds("Length was a bit strange here."
;
else
{
m_base = copy.m_base;
m_data = new T[m_length];
memset(m_data, 0, m_length * sizeof(T));
for(int i = 0; i < m_length; i++)
{
m_data = copy.m_data;
}
}
}
template <typename T>
Array<T>::~Array()
{
if(m_data)
delete [] m_data;
m_base = 0;
m_length = 0;
}
template <typename T>
Array<T>& Array<T>:
perator=(const Array& anArray)
{
m_length = anArray.m_length;
if(m_length < 0)
throw OutOfBounds("Length was wrong size."
;
else
{
m_base = anArray.m_base;
for(int i = 0; i < m_length; i++)
{
m_data = anArray.m_data;
}
}
return *this;
}
template <typename T>
const T* Array<T>::getData() const
{
return m_data;
}
template <typename T>
T& Array<T>:
perator [](int index)
{
if(index < m_base || index >= m_length + m_base)
throw OutOfBounds("Index out of Range exception"
;
else
return m_data[index - m_base];
}
template <typename T>
void Array<T>::setBase(int newBase)
{
m_base = newBase;
}
template <typename T>
void Array<T>::setLength(int newLength)
{
m_length = newLength;
if(m_length == 0)
{
if(m_data)
{
delete [] m_data;
}
m_data = 0;
}
else if(m_length < 0 )
{
m_data = 0;
throw OutOfBounds("Invalid length of your array here."
;
}
else
{
if(m_data)
{
delete [] m_data;
}
m_data = new T[m_length];
memset(m_data, 0, m_length * sizeof(T));
}
}
template <typename T>
int Array<T>::getBase() const
{
return m_base;
}
template <typename T>
int Array<T>::getLength() const
{
return m_length;
}
Here's a snip of my Stack class:
#include "template.h"
template <typename U, int capacity = 20>
class StackArray
{
public:
StackArray();
bool IsEmpty();
U& topEl();
U pop();
void push(U value);
private:
int getSize();
Array<U> *ra; <- my array
int size;
int top;
};
template <typename U, int capacity>
U StackArray<U>:
{
try
{
if(top != -1)
{
//this line complained aboot:
return ra[top];
top--;
}
else
throw OutOfBounds("Hey, can't pop!"
}
catch(OutOfBounds obj)
{
obj.PrintErrorNo();
}
}
Error Produced by VS .NET:
error C2440: 'return' cannot convert from 'Array<T>' to 'int' with [ T = int ]
Here's my array class, sorry for the lengthy spam
template <typename T>
class Array
{
public:
Array();
Array(int length, int base = 0);
Array(const Array& copy );
virtual ~Array();
Array& operator=(const Array& anArray);
T& operator[](int index);
const T* getData() const;
int getBase() const;
int getLength() const;
void setBase(int newBase);
void setLength(int newLength);
protected:
T* m_data;
int m_base;
int m_length;
};
template <typename T>
Array<T>::Array() : m_data(0), m_length(0), m_base(0)
{ }
template <typename T>
Array<T>::Array(int length, int base = 0)
{
m_length = length;
if(m_length < 0)
throw OutOfBounds("Length was wrong size"
else
{
m_base = base;
m_data = new T[m_length];
memset(m_data, 0, m_length * sizeof(T));
}
}
template <typename T>
Array<T>::Array(const Array& copy)
{
m_length = copy.m_length;
if(m_length < 0)
throw OutOfBounds("Length was a bit strange here."
else
{
m_base = copy.m_base;
m_data = new T[m_length];
memset(m_data, 0, m_length * sizeof(T));
for(int i = 0; i < m_length; i++)
{
m_data = copy.m_data;
}
}
}
template <typename T>
Array<T>::~Array()
{
if(m_data)
delete [] m_data;
m_base = 0;
m_length = 0;
}
template <typename T>
Array<T>& Array<T>:
{
m_length = anArray.m_length;
if(m_length < 0)
throw OutOfBounds("Length was wrong size."
else
{
m_base = anArray.m_base;
for(int i = 0; i < m_length; i++)
{
m_data = anArray.m_data;
}
}
return *this;
}
template <typename T>
const T* Array<T>::getData() const
{
return m_data;
}
template <typename T>
T& Array<T>:
{
if(index < m_base || index >= m_length + m_base)
throw OutOfBounds("Index out of Range exception"
else
return m_data[index - m_base];
}
template <typename T>
void Array<T>::setBase(int newBase)
{
m_base = newBase;
}
template <typename T>
void Array<T>::setLength(int newLength)
{
m_length = newLength;
if(m_length == 0)
{
if(m_data)
{
delete [] m_data;
}
m_data = 0;
}
else if(m_length < 0 )
{
m_data = 0;
throw OutOfBounds("Invalid length of your array here."
}
else
{
if(m_data)
{
delete [] m_data;
}
m_data = new T[m_length];
memset(m_data, 0, m_length * sizeof(T));
}
}
template <typename T>
int Array<T>::getBase() const
{
return m_base;
}
template <typename T>
int Array<T>::getLength() const
{
return m_length;
}