John500
asked on
Template classes - Declaring a 'List' variable
Given the header file code below, what would be the correct way to declare multiple Lists within main()?
I'm wondering if I should declare in the header file each 'List' type, something like 'OwnerList' would be used instead of the generic 'List' I now have:
template <class ListData>
class ListNode{
public:
// ListNode constructors
ListNode(){prev = next = NULL;};
// overloaded constructor
ListNode(ListNode<ListData > *LN1, // assign value to prev pointer
ListNode<ListData> *LN2, // assign value to next pointer
ListData * LD) // assign value to data member
{prev = LN1; next = LN2; Data = LD;};
// ListNode destructor
~ListNode();
// get next & previous ListNode pointers
ListNode<ListData> *GetNextListNode(){return next;};
ListNode<ListData> *GetPrevListNode(){return prev;};
// set next & previous ListNode pointers
void SetNextPtr(ListNode<ListDa ta> * LN){next = LN;};
void SetPrevPtr(ListNode<ListDa ta> * LN){prev = LN;};
// get ListNode data member
ListData * GetDataMember(ListNode<Lis tData> * LN){ return LN->Data;};
// set ListNode data member
void SetDataMember(ListData *LN){Data = LN;};
private:
ListNode<ListData> *prev;
ListNode<ListData> *next;
ListData * Data;
};
template <class ListData>
class List{
public:
ListNode<ListData> *AddListNode(const ListData &);
void DeleteList();
void DeleteListNode(ListNode<Li stData> *);
ListNode<ListData> *GetFirstListNode(){return head;};
ListNode<ListData> *GetLastListNode(){return tail;};
List(){head = tail = NULL;}
~List(){DeleteList();};
private:
ListNode<ListData> *head;
ListNode<ListData> *tail;
};
I'm wondering if I should declare in the header file each 'List' type, something like 'OwnerList' would be used instead of the generic 'List' I now have:
template <class ListData>
class ListNode{
public:
// ListNode constructors
ListNode(){prev = next = NULL;};
// overloaded constructor
ListNode(ListNode<ListData
ListNode<ListData> *LN2, // assign value to next pointer
ListData * LD) // assign value to data member
{prev = LN1; next = LN2; Data = LD;};
// ListNode destructor
~ListNode();
// get next & previous ListNode pointers
ListNode<ListData> *GetNextListNode(){return next;};
ListNode<ListData> *GetPrevListNode(){return prev;};
// set next & previous ListNode pointers
void SetNextPtr(ListNode<ListDa
void SetPrevPtr(ListNode<ListDa
// get ListNode data member
ListData * GetDataMember(ListNode<Lis
// set ListNode data member
void SetDataMember(ListData *LN){Data = LN;};
private:
ListNode<ListData> *prev;
ListNode<ListData> *next;
ListData * Data;
};
template <class ListData>
class List{
public:
ListNode<ListData> *AddListNode(const ListData &);
void DeleteList();
void DeleteListNode(ListNode<Li
ListNode<ListData> *GetFirstListNode(){return
ListNode<ListData> *GetLastListNode(){return tail;};
List(){head = tail = NULL;}
~List(){DeleteList();};
private:
ListNode<ListData> *head;
ListNode<ListData> *tail;
};
ASKER
GlennDean,
I did as you suggested and declared the following in main():
List<int> myIntList;
List<float> myFloatList;
------------------------
Have any ideas why I would get this error in the function below:
error C2228: left of '.GetNextListNode' must have class/struct/union type
template <class ListData>
void List<ListData>::DeleteList ()
{
// create a pointer to the ListNode head
ListNode<ListData> *CurPtr = head;
// ListNode pointer used to hold the value of CurPtr
ListNode<ListData> *temp = NULL;
while(CurPtr != NULL){
// store the pointer of CurPtr
temp = CurPtr;
// advance CurPtr to next ListNode
CurPtr = CurPtr.GetNextListNode();
// delete the memory block held by the ListNode
delete(GetDataMember(temp) );
// delete current ListNode
delete(temp);
}
head = NULL;
tail = NULL;
return;
}
I did as you suggested and declared the following in main():
List<int> myIntList;
List<float> myFloatList;
------------------------
Have any ideas why I would get this error in the function below:
error C2228: left of '.GetNextListNode' must have class/struct/union type
template <class ListData>
void List<ListData>::DeleteList
{
// create a pointer to the ListNode head
ListNode<ListData> *CurPtr = head;
// ListNode pointer used to hold the value of CurPtr
ListNode<ListData> *temp = NULL;
while(CurPtr != NULL){
// store the pointer of CurPtr
temp = CurPtr;
// advance CurPtr to next ListNode
CurPtr = CurPtr.GetNextListNode();
// delete the memory block held by the ListNode
delete(GetDataMember(temp)
// delete current ListNode
delete(temp);
}
head = NULL;
tail = NULL;
return;
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
List<int> myIntList;
List<float> myFloatList;
Replace ListData by anything you want. I didn't really understand your 2nd question, but on a quick glance at your templates it looks fine.
Glenn