Solved

Template classes - Declaring a 'List' variable

Posted on 2000-04-22
3
189 Views
Last Modified: 2010-04-02
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<ListData> * LN){next = LN;};
                  void SetPrevPtr(ListNode<ListData> * LN){prev = LN;};

                  // get ListNode data member
                  ListData * GetDataMember(ListNode<ListData> * 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<ListData> *);
                  ListNode<ListData> *GetFirstListNode(){return head;};
                  ListNode<ListData> *GetLastListNode(){return tail;};
                  List(){head = tail = NULL;}
                  ~List(){DeleteList();};


            private:
                  ListNode<ListData> *head;
                  ListNode<ListData> *tail;

      };
0
Comment
Question by:John500
  • 2
3 Comments
 
LVL 3

Expert Comment

by:GlennDean
ID: 2740783
I'm sure the answer to your question is more complex, but ...   Just write
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
0
 

Author Comment

by:John500
ID: 2740820
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;
      }
0
 
LVL 3

Accepted Solution

by:
GlennDean earned 50 total points
ID: 2740896
Ah, CurPtr is a pointer so change the . to a ->

   Glenn
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
how to understand recursion 12 230
Parsing XML instructions in Java/any other language.... 3 116
convert char array to number in c 5 86
Unable to start eclipse ? 17 154
Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

821 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question