Template Class - need to resolve an error in the header file

Greetings:

I'm trying to compile the code for a template class as it relates to a List structure.  I'm testing the template header declaration by using one line within main() but I'm getting the following error:

Error stated within VC++:

error LNK2001: unresolved external symbol "public: __thiscall
ListNode<int>::~ListNode<int>(void)" (??1?$ListNode@H@@QAE@XZ)

Error stated within Borlands:

Error MYDEFS.H 23: Invalid use of template 'ListNode<int>' in function
main()

This error occurs on the line which declares the 2nd ListNode() constructor or line 8:

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 LN->next;};
   ListNode<ListData> *GetPrevListNode(){return LN->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(){Data = <ListData>;};


  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();

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

};


void main()
{

   ListNode<int> LN;

      return;

}

Thanks!



John500Asked:
Who is Participating?
 
KangaRooConnect With a Mentor Commented:
Is this an exact copy past of your code?

>> void SetDataMember(){Data = <ListData>;};

And

>> ~List(DeleteList());

Destructor can not take arguments
0
 
WynCommented:
->error LNK2001: unresolved external symbol "public: __thiscall
ListNode<int>::~ListNode<int>(void)" (??1?$ListNode@H@@QAE@XZ)
========
This means the implementation of destructor of ListNode is unavailable...
Make sure you have include its library...
Otherwise,you should define it...
0
 
KangaRooCommented:
With templates it is common to have to #include it's definition (that is the code!) in all the .cpp files that use them. There are all kinds of strategies but the simplest is to copy the code to the header file that _declares_ the template classes.
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

 
KangaRooCommented:
If you have any questionn, let me know
0
 
John500Author Commented:
As stated, I needed to define a destructor for the ListNode class.  I also needed one for the List.  

I recompiled and have no errors using Borland but the following error in VC++:

error C2061: syntax error : identifier 'DeleteList'

This error occurs in the header file within the List class on line:

~List(DeleteList());

Do I need to state the variable type which DeleteList(type) takes as a parameter?

I have only a 'return' statement within main().  Below is the code for the header, ListNode & List destructors:

      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 LN->next;};
                  ListNode<ListData> *GetPrevListNode(){return LN->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(){Data = <ListData>;};


            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;

      };


// ListNode Destructor
      template <class ListData>
      ListNode<ListData>::~ListNode()
      {

            if(Data)delete Data;
            prev = NULL;
            next = NULL;

      }

// List Destructor
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
 
John500Author Commented:
Scratch the question about including the 'type' for DeleteList().  I was thinking about a similar function I have which deletes ListNodes not Lists:

template <class ListData>
      void List<ListData>::DeleteListNode(ListNode<ListData> *NodPtr)


....
0
 
John500Author Commented:
KangaRoo,

Yes, the line below is an exact copy.

>> void SetDataMember(){Data = <ListData>;};

I guess one thing wrong here is that I need the pointer:

<ListData> *

0
 
John500Author Commented:
KangaRoo,

What do you think about this:

SetDataMember(<ListData> *LN){Data = LN;};
0
 
John500Author Commented:
Ok, I made the following changes and things seem to be Ok.  Anything else stand out as bad?

void SetDataMember(ListData *LN){Data = LN;};

~List(){DeleteList();};


0
 
John500Author Commented:
KangaRoo,

Thanks for the two pointers.  

Now that I'm running the code in main() I'm having a problem on the line that goes:

*LNPtr1 = LNb;

When I say trouble I mean the program crashes "Unhandled exception.  Access Violation"

Here is the whole thing.

void main()
      {

             ListNode<Owner> LNa;
             ListNode<Owner> LNb;
             ListNode<Owner> *LNPtr1 = NULL;

             LNa.SetNextPtr(LNPtr1); // set next to NULL.

             ListNode<Owner> *LNPtr2 = LNa.GetNextListNode(); // Check that it gets NULL.

             *LNPtr1 = LNb;
             LNa.SetNextPtr(LNPtr1); // set next to LNb
             LNPtr2 = LNa.GetNextListNode(); // Check that it points to LBb.

             return;

      }
0
 
KangaRooCommented:
>> ListNode<Owner> *LNPtr1 = NULL;
>> [....]
>> *LNPtr1 = LNb;

The first statement initializes a ListNode* to NULL, iow, it is pointing at nothing. It is not uncommon to do so, a null pointer is shouting out "Don't derefence me!". Which is exactly what is happening in the second statement.  And since this is not an uncommon error, the OS has protected that area.

Note that you are want to assign one node to another. Since you've not defined operator=(), the compiler will make one that simply copies all data members. Now one of the datamembers is a pointer to am object, that will now be shared by two nodes, and both will eventually try to 'delete' that object. Ouch.
0
 
John500Author Commented:
Ok, so all is well, except that my test was not a valid test, right?
0
 
KangaRooCommented:
I'd have to investigate the code, until now we were looking to get it compiled.
0
 
John500Author Commented:
Ok, how about I open another question but I'm now more concerned with declaring a 'List' variable within main()
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.