I am creating a templated ArrayList class and I have the functions written but I cannot get it to compile. My arraylist.h file includes my arraylist.hpp right before the #endif. My arraylist.hpp gets the "'ArrayList' does not name a type" error for the destructor, copy constructor, and operator =. The other functions get errors saying "expected initalizer before '<' token. I have looked through a ton of forums covering the same issue but none of their solutions worked for me. Here is my code:
//Returns size of ArrayList
template<typename T>
int ArrayList<T>::size() const
{
return m_size;
}
//Returns first element in ArrayList
template<typename T>
const T& ArrayList<T>::first() const
{
if(m_size > 0)
return m_data[0];
else
return m_errobj;
}
//Returns r/w reference to element at position i
template<typename T>
T& ArrayList<T>::operator[](int i)
{
return m_data[i];
}
//Returns read-only reference to element at position i
template<typename T>
const T& ArrayList<T>::operator[](int i) const
{
return m_data[i];
}
//Returns the index of element T
template<typename T>
int ArrayList<T>::find(const T& x)
{
for(int k = 0; k < m_size; k++)
{
if(x == m_data[k])
return k;
}
return -1;
}
//Clears all values in the ArrayList as well as size and max values
template<typename T>
void ArrayList<T>::clear()
{
delete[] m_data;
m_data = NULL;
m_size = 0;
m_max = 0;
return;
}
//Puts value x at the end of ArrayList
template<typename T>
void ArrayList<T>::insert_back(const T& x)
{
if(m_size == m_max)
{
T* tmp = new T[m_max*2];
m_max = m_max * 2;
for(int k = 0; k < m_size; k++)
{
tmp[k] = m_data[k];
delete [] m_data;
m_data = tmp;
}
m_data[m_size] = x;
m_size++;
}
}
//Inserts the element x at the index i in the ArrayList
template<typename T>
void ArrayList<T>::insert(const T& x, int i)
{
if(i < 0 || i > m_size)
cout << "Sorry, that index is invalid.";
if(m_size == m_max)
{
T* tmp = new T[m_max*2];
m_max = m_max * 2;
for(int k = 0; k < m_size; k++)
{
tmp[k] = m_data[k];
delete [] m_data;
m_data = tmp;
}
for(int k = m_size; k > i; k--)
m_data[k] = m_data[k-1];
m_data[i] = x;
m_size++;
}
}
//Removes the element at index i in the ArrayList
template<typename T>
void ArrayList<T>::remove(int i)
{
if(i < 0 || i >= m_size)
cout << "Sorry, that index is invalid";
for(int k = i; k < m_size-1; k++)
m_data[k] = m_data[k+1];
m_size--;
return;
}
//Swaps the elements at indexes i and k
template<typename T>
void ArrayList<T>::swap(int i, int k)
{
T temp = m_data[i];
m_data[i] = m_data[k];
m_data[k] = temp;
return;
}
//Adds alist to the end of the current ArrayList
template<typename T>
void ArrayList<T>::append(const ArrayList<T>& alist)
{
int oldSize = m_size;
m_size+= alist.m_size;
for(int k = 0; k < alist.m_size; k++)
m_data[k+oldSize-1] = alist.m_data[k];
return;
}
//Removes all duplicate elements in the ArrayList
template<typename T>
void ArrayList<T>::purge()
{
for(int j = 0; j < m_size; j++)
{
for(int k = 1; k < m_size; k++)
{
if(m_data[j] == m_data[k])
remove(k);
}
}
return;
}
I think you'll get a practical response if you do the following rather than expect people to wade through all this stuff:
1. Use code tags so that your code appears with line numbers etc - like the purple boxes you see everywhere.
2. Give us enough code so we can run/compile the program. Presumably you have a test file? If so please include that too.
3. Show us a copy of the error messages you got and the the line numbers. It helps us to help you :)
As the others have said, there's not enough here to compile.
We need your class declaration.
I don't know if the < > bracketing your code was an attempt at using code tags.
You can find instructions on how to use code tags here: http://www.cplusplus.com/articles/jEywvCM9/
Hint: You can edit your post, highlight your code and press the <> formatting button.