Accessing members of a class within a template
Hi everybody -
I'm trying to develop simple list template that can create lists of various types of objects. My code is partially based on an example given in SAMS Teach Yourself C++ in 21 days.
The following code includes my header for the template and also a short driver program. The header alone compiles just fine, but when I compile it with main(), I get errors (see end of post):
Code:
#include <iostream>
using namespace std;
//begin template header
template <class Type>
class List
{
public:
List():head(0),tail(0), theCount(0) {std::cout<<"Constructing list...";}
virtual ~List();
void insert(Type value);
void append(Type value);
int is_present(Type value);
int is_empty() const {return head == 0;}
int count() const {return theCount;}
void displayList() const;
private:
class ListCell
{
public:
ListCell( Type value, ListCell * cell = 0): val(value), next(cell){}
Type val;
ListCell * next;
};
ListCell * head;
ListCell * tail;
int theCount;
};
//destructor
template<class Type>
List<Type>::~List()
{
ListCell * pCurrent = head;
while (pCurrent != 0)
{
ListCell * pTemp = pCurrent->next;
delete pCurrent;
pCurrent = pTemp;
}
}
//insert implementation - inserts a new value at the head of the list.
template<class Type>
void List<Type>::insert(Type value)
{
ListCell * pTemp = new ListCell(value, head);
if (head == 0) pTemp = tail;
pTemp = head;
theCount++;
}
//append implementation - inserts a new value at the tail of the list.
template<class Type>
void List<Type>::append(Type value)
{
ListCell * pTemp = new ListCell(value);
if (head == 0) pTemp = head;
else tail->next = pTemp;
tail = pTemp;
theCount++;
}
//is_present implementation - checks to see if a given value is already present in the list
template<class Type>
int List<Type>::is_present(Type value)
{
if (head == 0)
{
return 0;
}
ListCell * pCurrent = head;
while (pCurrent != 0)
{
if (*pCurrent.val == value)
return 1;
else
pCurrent = *pCurrent.next;
}
return 0;
}
//displayList implementation - displays all current members of the list
//assumes that any class/type passed to the template List has overloaded the << operator.
template<class Type>
void List<Type>::displayList() const
{
ListCell * pCurrent = head;
for (int i = 1; pCurrent != 0; i++)
{
cout<<i <<":" <<*pCurrent.val; //causes compiler error when instantiated in main()
pCurrent = *pCurrent.next; //causes compiler error when instantiated in main()
}
}
//end header
//driver program
int main()
{
int value;
List<int> theList;
cout<<"Enter a value to insert:";
cin>> value;
cin.ignore();
theList.insert(value);
cout<<"You have " <<theList.count() <<" members in your list."; //this line causes error
theList.displayList();
cin.get();
}
I'm using Dev-C++ 4.9.9.2. When I try to compile, I get the following errors:
Line 96: error: request for member `val' in `pCurrent', which is of non-class type `List<int>::ListCell*'
Line 97: error: request for member `next' in `pCurrent', which is of non-class type `List<int>::ListCell*'
Somehow I can't access the members of the ListCell class... any help would be appreciated.
Also, this is only my second thread on these forums - have I posted too much code?