Class Templates: Inheritance

So I am trying to implement Class templates with inheritance, and have a question about member functions. Here are my codes. I will address my question after the code.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
 #include<iostream>
using namespace std;

template<class T>
class LinkedList
{
	protected:
		
		struct ListNode
		{
			T value;
			ListNode *next;
			ListNode(T value1, ListNode *next1 = NULL)
			{
				value = value1;
				next = next1;
			}
		};
		ListNode *head;          //list head pointer
	public:
		LinkedList()			//constructor
		{
			head = NULL;
		}
		~LinkedList();			//destructor
		
		void add(T value);
		virtual void displayList();
};

template<class T>
void LinkedList<T>::add(T value)
{
	if (head == NULL)
	{
		head = new ListNode(value);
	}
	else
	{
		/*The list is not empty. So use
		nodePtr to traverse the list */
		ListNode *nodePtr = head;
		
		while(nodePtr->next != NULL)
			nodePtr = nodePtr->next;

		/*nodePtr->next is NULL so nodePtr points to the
		last node. Create a new node and put it after the 
		last node.*/
		nodePtr->next = new ListNode(value);
	}
}

template<class T>
void LinkedList<T>::displayList()
{
	//Start at the head of the list
	ListNode *nodePtr = head;

	while(nodePtr)
	{
		cout<<nodePtr->value<< ", ";
		nodePtr = nodePtr->next;
	}
}

template<class T>
LinkedList<T>::~LinkedList()
{
	ListNode *nodePtr = head;

	while(nodePtr != NULL)
	{
		//garbage keeps track of node to be deleted
		ListNode *garbage = nodePtr;

		//move on to the next node, if any
		nodePtr = nodePtr->next;

		//delete "garbage" node
		delete garbage;
	}
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include "LinkedList.h"
using namespace std;
//This class creates a linked list, sorted. Implements LinkedList<T>
template<class T>
class SortedLinkedList: LinkedList<T>
{
	public:
		void add(T number);
		void displayList();

};

template<class T>
void SortedLinkedList<T>::add(T number)
{
	ListNode *nodePtr, *previousNodePtr;

	if(head == NULL || head->value >= number)
	{
		// A new node goes at the beginning of the list
		head = new ListNode(number, head);
	}
	else
	{
		previousNodePtr = head;
		nodePtr = head->next;

		//find the insertion point
		while(nodePtr != NULL && nodePtr->value < number)
		{
			previousNodePtr = nodePtr;
			nodePtr = nodePtr->next;
		}

		//insert the new node just before nodePtr
		previousNodePtr->next =  new ListNode(number, nodePtr);
	}
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include"SortedLinkedList.h"
#include<iostream>
using namespace std;

//Test SortedLinkedList..LinkedList class works fine 

int main()
{
	SortedLinkedList<int> l1;
	l1.add(8);
	l1.add(6);
	l1.add(5);
	l1.add(7);
	

	l1.displayList();
	cout<<endl;
	return 0;
}


Well my question is, why won't my displayList() function work? I get the error:

1>main.obj : error LNK2019: unresolved external symbol "public: virtual void __thiscall SortedLinkedList<int>::displayList(void)" (?displayList@?$SortedLinkedList@H@@UAEXXZ) referenced in function _main
1>C:\Users\Anthony\Documents\C++\Singly-Linked Lists\Linked List Template\Debug\Linked List Template.exe : fatal error LNK1120: 1 unresolved externals

Do I have redefine displayList() in my SortedLinkedList.h? I know for a fact that if I was not doing templates, displayList(), should work, but for general purposes I want to make these classes generic. It would be a waste if I had to redefine it in SortedLinkedList
closed account (SECMoG1T)
Hiclass SortedLinkedList, class LinkedList are related by inheritance , SortedLinkedList inherites all function from it's base class by default, when a function is labeled virtual it means that each class in an inheritance chain can provide different definitions of this function, if in any of the derived classes the base class definition of the v-function will suffice the role then you shouldn't redeclare that function in the derived class interface, else

.. A complete definition of the function must be provided to override the inherited function,
If such is not available this results into an error.

Sol- remove line 9 on your second file.

I also doubt the snippet on your first file from line 45-50.
Last edited on
Whoops that was an accident, that function shouldnt be virtual. But even when I have
void displayList(); the code still will not work.

The snippet from 45-50 from LinkedList.h, does work:
1
2
3
4
5
6
7
       while(nodePtr->next != NULL)
			nodePtr = nodePtr->next;

		/*nodePtr->next is NULL so nodePtr points to the
		last node. Create a new node and put it after the 
		last node.*/
		nodePtr->next = new ListNode(value);


So my question still remains, how do can I access displayList()?
closed account (SECMoG1T)
You need to define it
1
2
3
4
5
 void SortedLinkedList<T>::displayList ()
  {
    //define this function
   }
   


Note:

immediately you declare a function in a derived class with a similar identifier to one or more functions inherited from the base class , then that function will overshadow all of the inherited functions with similar identifier even if they differ in number of parameters.

Overloading should be handled cautiously in inheritance , you can get around these problem by using the scope resolution operator or using the appropriate identifiers
f.e void displaySortedList ()

In your case , if you meant to use the base class function displayList (), then you should remove the declaration in your sortedlinkedlist class, if you needed your sortedLinkedList class to have an overload the you must define it.

Last edited on
Wow! Okay so I solved it myself. I forgot to include the class access specifier , public, in
class SortedLinkedList: public LinkedList

The previous code:
class SortedLinkedList: LinkedList
caused LinkedList to be a private class, so everything was private, so in the main function the SortedLinkedList object was unable to access the displayList() function.
Topic archived. No new replies allowed.