Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Trouble with templates!!

Status
Not open for further replies.

weevilofdoom

Programmer
Jan 28, 2003
91
US
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>::pop()
{
try
{
if(top != -1)
{
//this line complained aboot:
return ra[top];
top--;
}
else
throw OutOfBounds(&quot;Hey, can't pop!&quot;);
}
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(&quot;Length was wrong size&quot;);
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(&quot;Length was a bit strange here.&quot;);
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>::eek:perator=(const Array& anArray)
{
m_length = anArray.m_length;

if(m_length < 0)
throw OutOfBounds(&quot;Length was wrong size.&quot;);
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>::eek:perator [](int index)
{
if(index < m_base || index >= m_length + m_base)
throw OutOfBounds(&quot;Index out of Range exception&quot;);
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(&quot;Invalid length of your array here.&quot;);
}
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;
}
 
ra is an Array<U> *

ra[ top ] is therefore an Array<U>

Array<U> is not convertible to U
 
Hey! thanks a lot, I was pretty stumped with that, very helpful :D
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top