Inherited template function not being defined - c++

When I try to compile my code for main, my compiler keeps saying that mergeLists() is undefined. However, I expectedly defined it earlier (in the orderLinkedList class. In addition, I know I defined it as a member and I didn't put a calling object, but when I redefine it as a non-member it throws
/--------------------------------------------
In file included from main.cpp:4:0:
orderedLinkedList.h:204:6: note: candidate: template void mergeLists(orderedLinkedList&, orderedLinkedList&)
void mergeLists(orderedLinkedList & list1, orderedLinkedList & list2) {
^
orderedLinkedList.h:204:6: note: template argument deduction/substitution failed:
main.cpp:43:31: note: 'unorderedLinkedList' is not derived from 'orderedLinkedList'
mergeLists(list, otherList);
/------------------------------------------------
In addition to the other errors. What am I missing?
template <class Type>
void orderedLinkedList<Type>::mergeLists(orderedLinkedList<Type> & list1, orderedLinkedList<Type> & list2) {
if(list2.first != NULL) {
nodeType <Type> * current = list2.first;
nodeType <Type> * trail = NULL;
for(int cntr = 0; cntr <= list2.count; cntr++) {
list1.insert(current->info);
trail = current;
current = current->link;
delete trail;
}
list2.count = 0;
list2.first = list2.last = NULL;
}
}
#ifndef H_orderedListType
#define H_orderedListType
#include "linkedList.h"
using namespace std;
template <class Type>
class orderedLinkedList: public linkedListType<Type>
{
typedef linkedListType<Type> super;
using super::first;
using super::count;
using super::last;
public:
bool search(const Type& searchItem) const;
//Function to determine whether searchItem is in the list.
//Postcondition: Returns true if searchItem is in the list,
// otherwise the value false is returned.
void insert(const Type& newItem);
//Function to insert newItem in the list.
//Postcondition: first points to the new list, newItem
// is inserted at the proper place in the
// list, and count is incremented by 1.
void insertFirst(const Type& newItem);
//Function to insert newItem in the list.
//Because the resulting list must be sorted, newItem is
//inserted at the proper in the list.
//This function uses the function insert to insert newItem.
//Postcondition: first points to the new list, newItem is
// inserted at the proper in the list,
// and count is incremented by 1.
void insertLast(const Type& newItem);
//Function to insert newItem in the list.
//Because the resulting list must be sorted, newItem is
//inserted at the proper in the list.
//This function uses the function insert to insert newItem.
//Postcondition: first points to the new list, newItem is
// inserted at the proper in the list,
// and count is incremented by 1.
void deleteNode(const Type& deleteItem);
//Function to delete deleteItem from the list.
//Postcondition: If found, the node containing
// deleteItem is deleted from the list;
// first points to the first node of the
// new list, and count is decremented by 1.
// If deleteItem is not in the list, an
// appropriate message is printed.
//void mergeLists(orderedLinkedList<Type> & list1, orderedLinkedList<Type> & list2);
};
/*******************************************************************************
********************************************************************************
*******************************************************************************/
template <class Type>
bool orderedLinkedList<Type>::search(const Type& searchItem) const
{
bool found = false;
nodeType<Type> *current; //pointer to traverse the list
current = first; //start the search at the first node
while (current != NULL && !found)
if (current->info >= searchItem)
found = true;
else
current = current->link;
if (found)
found = (current->info == searchItem); //test for equality
return found;
}//end search
template <class Type>
void orderedLinkedList<Type>::insert(const Type& newItem)
{
nodeType<Type> *current; //pointer to traverse the list
nodeType<Type> *trailCurrent; //pointer just before current
nodeType<Type> *newNode; //pointer to create a node
bool found;
newNode = new nodeType<Type>; //create the node
newNode->info = newItem; //store newItem in the node
newNode->link = NULL; //set the link field of the node
//to nullptr
if (first == NULL) //Case 1
{
first = newNode;
last = newNode;
count++;
}
else
{
current = first;
found = false;
while (current != NULL && !found) //search the list
if (current->info >= newItem)
found = true;
else
{
trailCurrent = current;
current = current->link;
}
if (current == first) //Case 2
{
newNode->link = first;
first = newNode;
count++;
}
else //Case 3
{
trailCurrent->link = newNode;
newNode->link = current;
if (current == NULL)
last = newNode;
count++;
}
}//end else
}//end insert
template<class Type>
void orderedLinkedList<Type>::insertFirst(const Type& newItem)
{
insert(newItem);
}//end insertFirst
template<class Type>
void orderedLinkedList<Type>::insertLast(const Type& newItem)
{
insert(newItem);
}//end insertLast
template<class Type>
void orderedLinkedList<Type>::deleteNode(const Type& deleteItem)
{
nodeType<Type> *current; //pointer to traverse the list
nodeType<Type> *trailCurrent; //pointer just before current
bool found;
if (first == NULL) //Case 1
cout << "Cannot delete from an empty list." << endl;
else
{
current = first;
found = false;
while (current != NULL && !found) //search the list
if (current->info >= deleteItem)
found = true;
else
{
trailCurrent = current;
current = current->link;
}
if (current == NULL) //Case 4
cout << "The item to be deleted is not in the "
<< "list." << endl;
else
if (current->info == deleteItem) //the item to be
//deleted is in the list
{
if (first == current) //Case 2
{
first = first->link;
if (first == NULL)
last = NULL;
delete current;
}
else //Case 3
{
trailCurrent->link = current->link;
if (current == last)
last = trailCurrent;
delete current;
}
count--;
}
else //Case 4
cout << "The item to be deleted is not in the "
<< "list." << endl;
}
}//end deleteNode
template <class Type>
void mergeLists(orderedLinkedList<Type> & list1, orderedLinkedList<Type> & list2) {
if(list2.first != NULL) {
nodeType <Type> * current = list2.first;
nodeType <Type> * trail = NULL;
for(int cntr = 0; cntr <= list2.count; cntr++) {
list1.insert(current->info);
trail = current;
current = current->link;
delete trail;
}
list2.count = 0;
list2.first = list2.last = NULL;
}
}
#endif
My calling is such that mergeLists(list,otherlist); where list and other list are lists of type int

Related

Unordered Linked List (C++)

I'm trying to learn Linked List, but I encountered a problem.
below is the code for linkedlist.h file
#ifndef H_LinkedListType
#define H_LinkedListType
#include <iostream>
#include <cassert>
using namespace std;
//Definition of the node
template <class Type>
struct nodeType
{
Type info;
nodeType<Type> *link;
};
template <class Type>
class linkedListIterator
{
public:
linkedListIterator();
//Default constructor
//Postcondition: current = nullptr;
linkedListIterator(nodeType<Type> *ptr);
//Constructor with a parameter.
//Postcondition: current = ptr;
Type operator*();
//Function to overload the dereferencing operator *.
//Postcondition: Returns the info contained in the node.
linkedListIterator<Type> operator++();
//Overload the pre-increment operator.
//Postcondition: The iterator is advanced to the next
// node.
bool operator==(const linkedListIterator<Type>& right) const;
//Overload the equality operator.
//Postcondition: Returns true if this iterator is equal to
// the iterator specified by right,
// otherwise it returns the value false.
bool operator!=(const linkedListIterator<Type>& right) const;
//Overload the not equal to operator.
//Postcondition: Returns true if this iterator is not
// equal to the iterator specified by
// right; otherwise it returns the value
// false.
private:
nodeType<Type> *current; //pointer to point to the current
//node in the linked list
};
template <class Type>
linkedListIterator<Type>::linkedListIterator()
{
current = nullptr;
}
template <class Type>
linkedListIterator<Type>::
linkedListIterator(nodeType<Type> *ptr)
{
current = ptr;
}
template <class Type>
Type linkedListIterator<Type>::operator*()
{
return current->info;
}
template <class Type>
linkedListIterator<Type> linkedListIterator<Type>::
operator++()
{
current = current->link;
return *this;
}
template <class Type>
bool linkedListIterator<Type>::operator==
(const linkedListIterator<Type>& right) const
{
return (current == right.current);
}
template <class Type>
bool linkedListIterator<Type>::operator!=
(const linkedListIterator<Type>& right) const
{ return (current != right.current);
}
//***************** class linkedListType ****************
template <class Type>
class linkedListType
{
public:
const linkedListType<Type>& operator=
(const linkedListType<Type>&);
//Overload the assignment operator.
void initializeList();
//Initialize the list to an empty state.
//Postcondition: first = nullptr, last = nullptr,
// count = 0;
bool isEmptyList() const;
//Function to determine whether the list is empty.
//Postcondition: Returns true if the list is empty,
// otherwise it returns false.
void print() const;
//Function to output the data contained in each node.
//Postcondition: none
int length() const;
//Function to return the number of nodes in the list.
//Postcondition: The value of count is returned.
void destroyList();
//Function to delete all the nodes from the list.
//Postcondition: first = nullptr, last = nullptr,
// count = 0;
Type front() const;
//Function to return the first element of the list.
//Precondition: The list must exist and must not be
// empty.
//Postcondition: If the list is empty, the program
// terminates; otherwise, the first
// element of the list is returned.
Type back() const;
//Function to return the last element of the list.
//Precondition: The list must exist and must not be
// empty.
//Postcondition: If the list is empty, the program
// terminates; otherwise, the last
// element of the list is returned.
virtual bool search(const Type& searchItem) const = 0;
//Function to determine whether searchItem is in the list.
//Postcondition: Returns true if searchItem is in the
// list, otherwise the value false is
// returned.
virtual void insertFirst(const Type& newItem) = 0;
//Function to insert newItem at the beginning of the list.
//Postcondition: first points to the new list, newItem is
// inserted at the beginning of the list,
// last points to the last node in the list,
// and count is incremented by 1.
virtual void insertLast(const Type& newItem) = 0;
//Function to insert newItem at the end of the list.
//Postcondition: first points to the new list, newItem
// is inserted at the end of the list,
// last points to the last node in the
// list, and count is incremented by 1.
virtual void deleteNode(const Type& deleteItem) = 0;
//Function to delete deleteItem from the list.
//Postcondition: If found, the node containing
// deleteItem is deleted from the list.
// first points to the first node, last
// points to the last node of the updated
// list, and count is decremented by 1.
linkedListIterator<Type> begin();
//Function to return an iterator at the begining of
//the linked list.
//Postcondition: Returns an iterator such that current
// is set to first.
linkedListIterator<Type> end();
//Function to return an iterator one element past the
//last element of the linked list.
//Postcondition: Returns an iterator such that current
// is set to nullptr.
linkedListType();
//Default constructor
//Initializes the list to an empty state.
//Postcondition: first = nullptr, last = nullptr,
// count = 0;
linkedListType(const linkedListType<Type>& otherList);
//copy constructor
~linkedListType();
//Destructor
//Deletes all the nodes from the list.
//Postcondition: The list object is destroyed.
protected:
int count; //variable to store the number of
//elements in the list
nodeType<Type> *first; //pointer to the first node of the list
nodeType<Type> *last; //pointer to the last node of the list
private:
void copyList(const linkedListType<Type>& otherList);
//Function to make a copy of otherList.
//Postcondition: A copy of otherList is created and
// assigned to this list.
};
template <class Type>
bool linkedListType<Type>::isEmptyList() const
{
return (first == nullptr);
}
template <class Type>
linkedListType<Type>::linkedListType() //default constructor
{
first = nullptr;
last = nullptr;
count = 0;
}
template <class Type>
void linkedListType<Type>::destroyList()
{
nodeType<Type> *temp; //pointer to deallocate the memory
//occupied by the node
while (first != nullptr) //while there are nodes in
{ //the list
temp = first; //set temp to the current node
first = first->link; //advance first to the next node
delete temp; //deallocate the memory occupied by temp
}
last = nullptr; //initialize last to nullptr; first has
//already been set to nullptr by the while loop
count = 0;
}
template <class Type>
void linkedListType<Type>::initializeList()
{
destroyList(); //if the list has any nodes, delete them
}
template <class Type>
void linkedListType<Type>::print() const
{
nodeType<Type> *current; //pointer to traverse the list
current = first; //set current so that it points to
//the first node
while (current != nullptr) //while more data to print
{
cout << current->info << " ";
current = current->link;
}
}//end print
template <class Type>
int linkedListType<Type>::length() const
{
return count;
} //end length
template <class Type>
Type linkedListType<Type>::front() const
{
assert(first != nullptr);
return first->info; //return the info of the first node
}//end front
template <class Type>
Type linkedListType<Type>::back() const
{
assert(last != nullptr);
return last->info; //return the info of the last node
}//end back
template <class Type>
linkedListIterator<Type> linkedListType<Type>::begin()
{
linkedListIterator<Type> temp(first);
return temp;
}
template <class Type>
linkedListIterator<Type> linkedListType<Type>::end()
{
linkedListIterator<Type> temp(nullptr);
return temp;
}
template <class Type>
void linkedListType<Type>::copyList
(const linkedListType<Type>& otherList)
{
nodeType<Type> *newNode; //pointer to create a node
nodeType<Type> *current; //pointer to traverse the list
if (first != nullptr) //if the list is nonempty, make it empty
destroyList();
if (otherList.first == nullptr) //otherList is empty
{
first = nullptr;
last = nullptr;
count = 0;
}
else
{
current = otherList.first; //current points to the
//list to be copied
count = otherList.count;
//copy the first node
first = new nodeType<Type>; //create the node
first->info = current->info; //copy the info
first->link = nullptr; //set the link field of
//the node to nullptr
last = first; //make last point to the
//first node
current = current->link; //make current point to
//the next node
//copy the remaining list
while (current != nullptr)
{
newNode = new nodeType<Type>; //create a node
newNode->info = current->info; //copy the info
newNode->link = nullptr; //set the link of
//newNode to nullptr
last->link = newNode; //attach newNode after last
last = newNode; //make last point to
//the actual last node
current = current->link; //make current point
//to the next node
}//end while
}//end else
}//end copyList
template <class Type>
linkedListType<Type>::~linkedListType() //destructor
{
destroyList();
}//end destructor
template <class Type>
linkedListType<Type>::linkedListType
(const linkedListType<Type>& otherList)
{
first = nullptr;
copyList(otherList);
}//end copy constructor
//overload the assignment operator
template <class Type>
const linkedListType<Type>& linkedListType<Type>::operator=
(const linkedListType<Type>& otherList)
{
if (this != &otherList) //avoid self-copy
{
copyList(otherList);
}//end else
return *this;
}
#endif
Below is the code for the testProgLinkedList.cpp
//This program tests various operation of a linked list
//34 62 21 90 66 53 88 24 10 -999
#include <iostream> //Line 1
#include "unorderedLinkedList.h" //Line 2
using namespace std; //Line 3
int main() //Line 4
{ //Line 5
unorderedLinkedList<int> list1, list2; //Line 6
int num; //Line 7
cout << "Line 8: Enter integers ending "
<< "with -999" << endl; //Line 8
cin >> num; //Line 9
while (num != -999) //Line 10
{ //Line 11
list1.insertLast(num); //Line 12
cin >> num; //Line 13
} //Line 14
cout << endl; //Line 15
cout << "Line 16: list1: "; //Line 16
list1.print(); //Line 17
cout << endl; //Line 18
cout << "Line 19: Length of list1: "
<< list1.length() << endl; //Line 19
list2 = list1; //test the assignment operator Line 20
cout << "Line 21: list2: "; //Line 21
list2.print(); //Line 22
cout << endl; //Line 23
cout << "Line 24: Length of list2: "
<< list2.length() << endl; //Line 24
cout << "Line 25: Enter the number to be "
<< "deleted: "; //Line 25
cin >> num; //Line 26
cout << endl; //Line 27
list2.deleteNode(num); //Line 28
cout << "Line 29: After deleting " << num
<< " list2: " << endl; //Line 29
list2.print(); //Line 30
cout << endl; //Line 31
cout << "Line 32: Length of list2: "
<< list2.length() << endl; //Line 32
cout << endl << "Line 33: Output list1 "
<< "using an iterator" << endl; //Line 33
linkedListIterator<int> it; //Line 34
for (it = list1.begin(); it != list1.end();
++it) //Line 35
cout << *it << " "; //Line 36
cout << endl; //Line 37
return 0; //Line 38
} //Line 39
Below is the code for the unorderedLinkList.h file
#ifndef H_UnorderedLinkedList
#define H_UnorderedLinkedList
#include "linkedList.h"
using namespace std;
template <class Type>
class unorderedLinkedList: public linkedListType<Type>
{
public:
bool search(const Type& searchItem) const;
//Function to determine whether searchItem is in the list.
//Postcondition: Returns true if searchItem is in the
// list, otherwise the value false is
// returned.
void insertFirst(const Type& newItem);
//Function to insert newItem at the beginning of the list.
//Postcondition: first points to the new list, newItem is
// inserted at the beginning of the list,
// last points to the last node in the
// list, and count is incremented by 1.
void insertLast(const Type& newItem);
//Function to insert newItem at the end of the list.
//Postcondition: first points to the new list, newItem
// is inserted at the end of the list,
// last points to the last node in the
// list, and count is incremented by 1.
void deleteNode(const Type& deleteItem);
//Function to delete deleteItem from the list.
//Postcondition: If found, the node containing
// deleteItem is deleted from the list.
// first points to the first node, last
// points to the last node of the updated
// list, and count is decremented by 1.
};
template <class Type>
bool unorderedLinkedList<Type>::
search(const Type& searchItem) const
{
nodeType<Type> *current; //pointer to traverse the list
bool found = false;
current = first; //set current to point to the first
//node in the list
while (current != nullptr && !found) //search the list
if (current->info == searchItem) //searchItem is found
found = true;
else
current = current->link; //make current point to
//the next node
return found;
}//end search
template <class Type>
void unorderedLinkedList<Type>::insertFirst(const Type& newItem)
{
nodeType<Type> *newNode; //pointer to create the new node
newNode = new nodeType<Type>; //create the new node
newNode->info = newItem; //store the new item in the node
newNode->link = first; //insert newNode before first
first = newNode; //make first point to the
//actual first node
count++; //increment count
if (last == nullptr) //if the list was empty, newNode is also
//the last node in the list
last = newNode;
}//end insertFirst
template <class Type>
void unorderedLinkedList<Type>::insertLast(const Type& newItem)
{
nodeType<Type> *newNode; //pointer to create the new node
newNode = new nodeType<Type>; //create the new node
newNode->info = newItem; //store the new item in the node
newNode->link = nullptr; //set the link field of newNode
//to nullptr
if (first == nullptr) //if the list is empty, newNode is
//both the first and last node
{
first = newNode;
last = newNode;
count++; //increment count
}
else //the list is not empty, insert newNode after last
{
last->link = newNode; //insert newNode after last
last = newNode; //make last point to the actual
//last node in the list
count++; //increment count
}
}//end insertLast
template <class Type>
void unorderedLinkedList<Type>::deleteNode(const Type& deleteItem)
{
nodeType<Type> *current; //pointer to traverse the list
nodeType<Type> *trailCurrent; //pointer just before current
bool found;
if (first == nullptr) //Case 1; the list is empty.
cout << "Cannot delete from an empty list."
<< endl;
else
{
if (first->info == deleteItem) //Case 2
{
current = first;
first = first->link;
count--;
if (first == nullptr) //the list has only one node
last = nullptr;
delete current;
}
else //search the list for the node with the given info
{
found = false;
trailCurrent = first; //set trailCurrent to point
//to the first node
current = first->link; //set current to point to
//the second node
while (current != nullptr && !found)
{
if (current->info != deleteItem)
{
trailCurrent = current;
current = current-> link;
}
else
found = true;
}//end while
if (found) //Case 3; if found, delete the node
{
trailCurrent->link = current->link;
count--;
if (last == current) //node to be deleted
//was the last node
last = trailCurrent; //update the value
//of last
delete current; //delete the node from the list
}
else
cout << "The item to be deleted is not in "
<< "the list." << endl;
}//end else
}//end else
}//end deleteNode
#endif
But when I compiled the code it showed error messages as seen below:
I don't know how to fix the errors as I am new to coding. Please help me.
Error messages
Generally speaking you can ignore all the errors after the first couple. Fix errors one at a time starting with the first.
This line
current = first; //set current to point to the first
//node in the list
generates an error which says that the compiler doesn't know what first is. Now it looks like there is a declaration of first in linkedListType<Type> but the compiler doesn't find it. Why? Well that is complicated, but basically it is because your class is a template and the name is defined in a parent template class. Fortunately there is a simple fix, just add this->
current = this->first; //set current to point to the first
//node in the list
More details here, but be warned it's complicated.

no suitable conversion function from "orderedLinkedList<int>" to "const int" exists

I am having an issue with a homework assignment and for the life of me I cannot figure out where I am making the mistake. We are tasked with writing a function that inserts the nodes of binary tree into an ordered linked list. Below are the header files and the main method. Literally any help at this point would greatly appreciated as I am just spinning in circles now.
Error Message: no suitable conversion function from "orderedLinkedList" to "const int" exists
Project: Ch19_Ex9 File: Ch19_Ex9.cpp Line: 33:
Ch19_Ex9.cpp:
//Data
//68 43 10 56 77 82 61 82 33 56 72 66 99 88 12 6 7 21 -999
#include "binarySearchTree.h"
#include "orderedLinkedList.h"
#include "pch.h"
#include <iostream>
using namespace std;
int main()
{
binarySearchTree<int> treeRoot;
orderedLinkedList<int> newList;
int num;
cout << "Enter numbers ending with -999" << endl;
cin >> num;
while (num != -999)
{
treeRoot.insert(num);
cin >> num;
}
cout << endl << "Tree nodes in inorder: ";
treeRoot.inorderTraversal();
cout << endl;
cout << "Tree Height: " << treeRoot.treeHeight()
<< endl;
treeRoot.createList(newList); //HERE IS THE LINE WITH ISSUE
cout << "newList: ";
newList.print();
cout << endl;
system("pause");
return 0;
}
binarySearchTree.h:
//Header File Binary Search Tree
#include <iostream>
#include "binaryTree.h"
using namespace std;
template <class Type>
class binarySearchTree : public binaryTreeType<Type>
{
public:
bool search(const Type& searchItem) const;
//Function to determine if searchItem is in the binary
//search tree.
//Postcondition: Returns true if searchItem is found in
// the binary search tree; otherwise,
// returns false.
void insert(const Type& insertItem);
//Function to insert insertItem in the binary search tree.
//Postcondition: If there is no node in the binary search
// tree that has the same info as
// insertItem, a node with the info
// insertItem is created and inserted in the
// binary search tree.
void deleteNode(const Type& deleteItem);
//Function to delete deleteItem from the binary search tree
//Postcondition: If a node with the same info as deleteItem
// is found, it is deleted from the binary
// search tree.
// If the binary tree is empty or deleteItem
// is not in the binary tree, an appropriate
// message is printed.
void createList(const Type& addItem);
private:
void deleteFromTree(nodeType<Type>* &p);
//Function to delete the node to which p points is
//deleted from the binary search tree.
//Postcondition: The node to which p points is deleted
// from the binary search tree.
};
template <class Type>
bool binarySearchTree<Type>::search
(const Type& searchItem) const
{
nodeType<Type> *current;
bool found = false;
if (root == nullptrptr)
cout << "Cannot search an empty tree." << endl;
else
{
current = root;
while (current != nullptrptr && !found)
{
if (current->info == searchItem)
found = true;
else if (current->info > searchItem)
current = current->lLink;
else
current = current->rLink;
}//end while
}//end else
return found;
}//end search
template <class Type>
void binarySearchTree<Type>::insert
(const Type& insertItem)
{
nodeType<Type> *current; //pointer to traverse the tree
nodeType<Type> *trailCurrent; //pointer behind current
nodeType<Type> *newNode; //pointer to create the node
newNode = new nodeType<Type>;
newNode->info = insertItem;
newNode->lLink = nullptrptr;
newNode->rLink = nullptrptr;
if (root == nullptrptr)
root = newNode;
else
{
current = root;
while (current != nullptrptr)
{
trailCurrent = current;
if (current->info == insertItem)
{
cout << "The item to be inserted is already ";
cout << "in the tree -- duplicates are not "
<< "allowed." << endl;
return;
}
else if (current->info > insertItem)
current = current->lLink;
else
current = current->rLink;
}//end while
if (trailCurrent->info > insertItem)
trailCurrent->lLink = newNode;
else
trailCurrent->rLink = newNode;
}
}//end insert
template <class Type>
void binarySearchTree<Type>::deleteNode
(const Type& deleteItem)
{
nodeType<Type> *current; //pointer to traverse the tree
nodeType<Type> *trailCurrent; //pointer behind current
bool found = false;
if (root == nullptr)
cout << "Cannot delete from an empty tree."
<< endl;
else
{
current = root;
trailCurrent = root;
while (current != nullptr && !found)
{
if (current->info == deleteItem)
found = true;
else
{
trailCurrent = current;
if (current->info > deleteItem)
current = current->lLink;
else
current = current->rLink;
}
}//end while
if (current == nullptr)
cout << "The item to be deleted is not in the tree."
<< endl;
else if (found)
{
if (current == root)
deleteFromTree(root);
else if (trailCurrent->info > deleteItem)
deleteFromTree(trailCurrent->lLink);
else
deleteFromTree(trailCurrent->rLink);
}
else
cout << "The item to be deleted is not in the tree."
<< endl;
}
} //end deleteNode
template <class Type>
void binarySearchTree<Type>::deleteFromTree
(nodeType<Type>* &p)
{
nodeType<Type> *current; //pointer to traverse the tree
nodeType<Type> *trailCurrent; //pointer behind current
nodeType<Type> *temp; //pointer to delete the node
if (p == nullptr)
cout << "Error: The node to be deleted does not exist."
<< endl;
else if (p->lLink == nullptr && p->rLink == nullptr)
{
temp = p;
p = nullptr;
delete temp;
}
else if (p->lLink == nullptr)
{
temp = p;
p = temp->rLink;
delete temp;
}
else if (p->rLink == nullptr)
{
temp = p;
p = temp->lLink;
delete temp;
}
else
{
current = p->lLink;
trailCurrent = nullptr;
while (current->rLink != nullptr)
{
trailCurrent = current;
current = current->rLink;
}//end while
p->info = current->info;
if (trailCurrent == nullptr) //current did not move;
//current == p->lLink; adjust p
p->lLink = current->lLink;
else
trailCurrent->rLink = current->lLink;
delete current;
}//end else
} //end deleteFromTree
template <class Type>
void binarySearchTree<Type>::createList(const Type& addItem)
{
nodeType<Type> *current;
nodeType<Type> *trailCurrent;
nodeType<Type> *newNode;
newNode = new nodeType <Type>;
newNode->info = createItem;
newNode->lLink = nullptr;
newNode->rLink = nullptr;
if (root == nullptr)
{
root = newNode;
}
}
EDIT: adding orderedLinkedList.h:
#include "linkedList.h"
using namespace std;
template <class Type>
class orderedLinkedList : public linkedListType<Type>
{
public:
bool search(const Type& searchItem) const;
void insert(const Type& newItem);
void insertFirst(const Type& newItem);
void insertLast(const Type& deleteItem);
void deleteNode(const Type& deleteItem);
};
template <class Type>
bool orderedLinkedList<Type>::search(const Type& searchItem) const
{
nodeType<Type> *current; //pointer to traverse the list
bool found = false;
current = first; //set current to point to the first
//node in the list
while (current != nullptr && !found) //search the list
if (current->info == searchItem) //searchItem is found
found = true;
else
current = current->link; //make current point to
//the next node
return found;
};
template <class Type>
void orderedLinkedList<Type>::insert(const Type& newItem)
{
};
template <class Type>
void orderedLinkedList<Type>::insertFirst(const Type& newItem)
{
nodeType<Type> *newNode; //pointer to create the new node
newNode = new nodeType<Type>; //create the new node
newNode->info = newItem; //store the new item in the node
newNode->link = first; //insert newNode before first
first = newNode; //make first point to the
//actual first node
count++; //increment count
if (last == nullptr) //if the list was empty, newNode is also
//the last node in the list
last = newNode;
};
template <class Type>
void orderedLinkedList<Type>::insertLast(const Type& deleteItem)
{
nodeType<Type> *newNode; //pointer to create the new node
newNode = new nodeType<Type>; //create the new node
newNode->info = newItem; //store the new item in the node
newNode->link = nullptr; //set the link field of newNode
//to nullptr
if (first == nullptr) //if the list is empty, newNode is
//both the first and last node
{
first = newNode;
last = newNode;
count++; //increment count
}
else //the list is not empty, insert newNode after last
{
last->link = newNode; //insert newNode after last
last = newNode; //make last point to the actual
//last node in the list
count++; //increment count
}
};
template <class Type>
void orderedLinkedList<Type>::deleteNode(const Type& deleteItem)
{
nodeType<Type> *current;
nodeType<Type> *trailCurrent;
bool found;
if (first == nullptr)
{
cout << "Cannot delete from an empty list."
<< endl;
}
else
{
if (first->info == deleteItem)
{
current = first;
first = first->link;
count--;
if (first == nullptr)
{
last = nullptr;
delete current;
}
else
{
found = false;
trailCurrent = first;
current = first->link;
while (current != nullptr && !found)
{
if (current->info != deleteItem)
{
trailCurrent = current;
current = current->link;
}
else
{
found = true;
}
}
if (found)
{
trailCurrent->link = current->link;
count--;
if (last == current)
{
last = trailCurrent;
}
delete current;
}
else
cout << "The item to be deleted is not in "
<< "the list." << endl;
}
}
}
}

Linker errors with class template

I'm testing ADT linked list implementation. It's from my college text from a previous semester and one of my current assignments requires me to use it. and it is derived from a more general linked List Type class. I've been fighting this problem for hours, and cant figure out why it wont link.
this is main:
#include <iostream>
#include <cmath>
#include "unorderedlinkedlist.h"
using namespace std;
int main()
{
unorderedLinkedList<int> newList;
for (int i = 1; i<=10;i++)
newList.insertLast(pow(2,i));
newList.print();
return 0;
}
this is the header for the derived class:
#ifndef UNORDEREDLINKEDLIST_H
#define UNORDEREDLINKEDLIST_H
#include <iostream>
#include "linkedlisttype.h"
using namespace std;
template <class Type>
class unorderedLinkedList : public linkedListType<Type>
{
public:
/// Check to see if item already exists in the list
bool search(const Type& searchItem) const;
/// insert newItem at the beginning of the list
void insertFirst(const Type& newItem);
///function to insert newItem at the end of the list
void insertLast(const Type& newItem);
/// delese deleteItem from the list
void deleteNode(const Type& deleteItem);
protected:
private:
};
template <class Type>
bool unorderedLinkedList<Type>::search(const Type& searchItem) const
{
nodeType<Type> *current;
bool found = false;
current = linkedListType<Type>::first;
while (current != NULL && !found)
if (current->info == searchItem)
found = true;
else current = current->link;
return found;
}
template <class Type>
void unorderedLinkedList<Type>::insertFirst(const Type& newItem)
{
nodeType<Type> *newNode;
newNode = new nodeType<Type>;
newNode->info = newItem;
newNode->link = linkedListType<Type>::first;
linkedListType<Type>::first = newNode;
linkedListType<Type>::count++;
if (linkedListType<Type>::last == NULL)
linkedListType<Type>::last = newNode;
}
template <class Type>
void unorderedLinkedList<Type>::insertLast(const Type& newItem)
{
nodeType<Type> *newNode;
newNode = new nodeType<Type>;
newNode->info = newItem;
newNode->link = NULL;
if (linkedListType<Type>::first == NULL)
{
linkedListType<Type>::first = newNode;
linkedListType<Type>::last = newNode;
linkedListType<Type>::count++;
}
else
{
linkedListType<Type>::last->link = newNode;
linkedListType<Type>::last = newNode;
linkedListType<Type>::count++;
}
}
template <class Type>
void unorderedLinkedList<Type>::deleteNode(const Type& deleteItem)
{
nodeType<Type> *current;
nodeType<Type> *trailCurrent;
bool found;
if (linkedListType<Type>::first == NULL)
cout << "You cannot delete soemthing from an empty list." << endl;
else
{
if (linkedListType<Type>::first ->info == deleteItem)
{
current = linkedListType<Type>::first;
linkedListType<Type>::first = linkedListType<Type>::first->link;
linkedListType<Type>::count--;
if (linkedListType<Type>::first == NULL)
linkedListType<Type>::last = NULL;
delete current;
}
else
{
found = false;
trailCurrent = linkedListType<Type>::first;
current = linkedListType<Type>::first->link;
while (current != NULL && !found)
{
if(current->info != deleteItem)
{
trailCurrent = current;
current = current->link;
}
else
found = true;
}
if(found)
{
trailCurrent->link = current->link;
linkedListType<Type>::count--;
if (linkedListType<Type>::last == current)
linkedListType<Type>::last = trailCurrent;
delete current;
}
else
cout << "the item to be deleted is not in the list." << endl;
}
}
}
#endif // UNORDEREDLINKEDLIST_H
Linker errors:
/tmp/cc9ozgPv.o:
(.rodata._ZTV14linkedListTypeIiE[_ZTV14linkedListTypeIiE]+0x20): undefined reference to linkedListType<int>::search(int const&) const'
/tmp/cc9ozgPv.o:(.rodata._ZTV14linkedListTypeIiE[_ZTV14linkedListTypeIiE]+0x28): undefined reference tolinkedListType::insertFirst(int const&)'
/tmp/cc9ozgPv.o:(.rodata._ZTV14linkedListTypeIiE[_ZTV14linkedListTypeIiE]+0x30): undefined reference to linkedListType<int>::insertLast(int const&)'
/tmp/cc9ozgPv.o:(.rodata._ZTV14linkedListTypeIiE[_ZTV14linkedListTypeIiE]+0x38): undefined reference tolinkedListType::deleteNode(int const&)'
collect2: error: ld returned 1 exit status

linked list function c++ "which is of non-class type 'int'"?

below is the problem in one of the header of a code:
#include <iostream>
#include <fstream>
#include <string>
#include "extPersonType.h"
#include "orderedLinkedList.h"
using namespace std;
class addressBookType: public orderedLinkedListType
{
public:
void print() const;
void printNameInTheMonth(int month);
void printInfoOf(string lName);
void printNamesWithStatus(string status);
void printNamesBetweenLastNames(string last1,
string last2);
void insertNode(const extPersonType& eP);
void searchName(string lName);
void saveData(ofstream&);
addressBookType();
private:
nodeType* searchList(string lName);
};
void addressBookType::print() const
{
nodeType* current = first;
while (current != NULL)
{
current->info.printInfo();
cout << endl;
current= current->link;
}
}
void addressBookType::printNameInTheMonth(int month)
{
nodeType* current = first;
while(current != NULL)
{
if (current->info.isMonth(month))
{
current->info.print();
cout << endl;
}
current = current->link;
}
}
void addressBookType::printInfoOf(string lName)
{
nodeType* location = searchList(lName);
if (location != NULL)
location->info.printInfo();
else
cout << lName << " is not in address book." << endl;
}
void addressBookType::printNamesWithStatus(string status)
{
nodeType* current = first;
while (current != NULL)
{
if (current->info.isStatus(status))
{
current->info.print();
cout << endl;
}
current = current->link;
}
}
void addressBookType::printNamesBetweenLastNames(string last1,
string last2)
{
string lName;
nodeType* current = first;
while(current != NULL)
{
lName = current->info.getLastName();
if (last1 <= lName && lName <= last2)
{
current->info.print();
cout << endl;
}
current = current->link;
}
}
void addressBookType::insertNode(const extPersonType& eP)
{
orderedLinkedListType::insertNode(eP);
}
void addressBookType::searchName(string lName)
{
nodeType* location = searchList(lName);
if (location != NULL)
cout << lName << " is in the address book" << endl;
else
cout << lName << " is not in the address book" << endl;
}
nodeType* addressBookType::searchList(string lName)
{
nodeType* current = first;
bool found = false;
while (current != NULL)
{
if (current->info.isLastName(lName))
{
found = true;
break;
}
current = current->link;
}
return current;
}
void addressBookType::saveData(ofstream& outFile)
{
string firstN;
string lastN;
int month;
int day;
int year;
string street;
string city;
string state;
string zip;
string phone;
string pStatus;
nodeType* current = first;
while (current != NULL)
{
current->info.getDOB(month, day, year);
current->info.getAddress(street,city,state,zip);
current->info.getPhoneNumber();
current->info.getStatus();
outFile << current->info.getFirstName() << " "
<< current->info.getLastName() << endl;
outFile << month << " " << day << " " << year << endl;
outFile << street << endl << city << endl << state << endl << zip
<< endl;
outFile << current->info.getPhoneNumber() << endl
<< current->info.getStatus() << endl;
current = current->link;
}
}
addressBookType::addressBookType()
{
}
these are the two headers file that i suspect that has a problem with it but they have NO ERRORS at all
#include <iostream>
#include "linkedList.h"
using namespace std;
class orderedLinkedListType: public linkedListType
{
public:
bool search(const int& searchItem) const;
//Function to determine whether searchItem is in the list
//Postcondition: Returns true if searchItem is in the list;
// otherwise, the value false is returned.
void insertNode(const int& newItem);
//Function to insert newItem in the list
//Postcondition: first points to the new list and
// newItem is inserted at the proper place in the list
void deleteNode(const int& deleteItem);
//Function to delete deleteItem from the list
//Postcondition: If found, then the node containing the
// deleteItem is deleted from the list;
// first points to the first node of
// the new list
// If deleteItem is not in the list,
// an appropriate message is printed
void printListReverse() const;
//This function prints the list in reverse order
//Because the original list is in ascending order, the
//elements will be printed in descending order
private:
void reversePrint(nodeType *current) const;
//This function is called by the public member
//function to print the list in reverse order
};
bool orderedLinkedListType::search(const int& searchItem) const
{
bool found = false;
nodeType *current; //pointer to traverse the list
current = first; //start the search at the first node
while (current != NULL && !found)
if (current->info >= searchItem)
found = true;
else
current = current->link;
if (found)
found = (current->info == searchItem); //test for equality
return found;
}//end search
void orderedLinkedListType::insertNode(const int& newitem)
{
nodeType *current; //pointer to traverse the list
nodeType *trailCurrent; //pointer just before current
nodeType *newNode; //pointer to create a node
bool found;
newNode = new nodeType; //create the node
assert(newNode != NULL);
newNode->info = newitem; //store newitem in the node
newNode->link = NULL; //set the link field of the node
//to NULL
if (first == NULL) //Case 1
{
first = newNode;
count++;
}
else
{
current = first;
found = false;
while (current != NULL && !found) //search the list
if (current->info >= newitem)
found = true;
else
{
trailCurrent = current;
current = current->link;
}
if (current == first) //Case 2
{
newNode->link = first;
first = newNode;
count++;
}
else //Case 3
{
trailCurrent->link = newNode;
newNode->link = current;
count++;
}
}//end else
}//end insertNode
void orderedLinkedListType::deleteNode(const int& deleteItem)
{
nodeType *current; //pointer to traverse the list
nodeType *trailCurrent; //pointer just before current
bool found;
if (first == NULL) //Case 1
cout << "Cannot delete from an empty list." << endl;
else
{
current = first;
found = false;
while (current != NULL && !found) //search the list
if (current->info >= deleteItem)
found = true;
else
{
trailCurrent = current;
current = current->link;
}
if (current == NULL) //Case 4
cout << "The item to be deleted is not in the "
<< "list." << endl;
else
if (current->info == deleteItem) //item to be
//deleted is in the list
{
if (first == current) //Case 2
{
first = first->link;
delete current;
}
else //Case 3
{
trailCurrent->link = current->link;
delete current;
}
count--;
}
else //Case 4
cout << "Item to be deleted is not in the "
<< "list." << endl;
}
} //end deleteNode
void orderedLinkedListType::reversePrint
(nodeType *current) const
{
if (current != NULL)
{
reversePrint(current->link); //print the tail
cout << current->info << " "; //print the node
}
}
void orderedLinkedListType::printListReverse() const
{
reversePrint(first);
cout << endl;
}
this is the second one:
#include <iostream>
#include <cassert>
#include <assert.h>
using namespace std;
//Definition of the node
struct nodeType
{
int info;
nodeType *link;
};
class linkedListType
{
public:
const linkedListType& operator=
(const linkedListType&);
//Overload the assignment operator.
void initializeList();
//Initialize the list to an empty state.
//Postcondition: first = NULL, last = NULL, count = 0;
bool isEmptyList() const;
//Function to determine whether the list is empty.
//Postcondition: Returns true if the list is empty,
// otherwise it returns false.
void print() const;
//Function to output the data contained in each node.
//Postcondition: none
int length() const;
//Function to return the number of nodes in the list.
//Postcondition: The value of count is returned.
void destroyList();
//Function to delete all the nodes from the list.
//Postcondition: first = NULL, last = NULL, count = 0;
int front() const;
//Function to return the first element of the list.
//Precondition: The list must exist and must not be
// empty.
//Postcondition: If the list is empty, the program
// terminates; otherwise, the first
// element of the list is returned.
int back()const;
//Function to return the last element of the list.
//Precondition: The list must exist and must not be
// empty.
//Postcondition: If the list is empty, the program
// terminates; otherwise, the last
// element of the list is returned.
bool search(const int& searchItem) const;
//Function to determine whether searchItem is in the list.
//Postcondition: Returns true if searchItem is in the
// list, otherwise the value false is
// returned.
void insertFirst(const int& newItem);
//Function to insert newItem at the beginning of the list.
//Postcondition: first points to the new list, newItem is
// inserted at the beginning of the list,
// last points to the last node in the list,
// and count is incremented by 1.
void insertLast(const int& newItem);
//Function to insert newItem at the end of the list.
//Postcondition: first points to the new list, newItem
// is inserted at the end of the list,
// last points to the last node in the list,
// and count is incremented by 1.
void deleteNode(const int& deleteItem);
//Function to delete deleteItem from the list.
//Postcondition: If found, the node containing
// deleteItem is deleted from the list.
// first points to the first node, last
// points to the last node of the updated
// list, and count is decremented by 1.
linkedListType();
//default constructor
//Initializes the list to an empty state.
//Postcondition: first = NULL, last = NULL, count = 0;
linkedListType(const linkedListType& otherList);
//copy constructor
~linkedListType();
//destructor
//Deletes all the nodes from the list.
//Postcondition: The list object is destroyed.
protected:
int count; //variable to store the number of
//elements in the list
nodeType *first; //pointer to the first node of the list
nodeType *last; //pointer to the last node of the list
private:
void copyList(const linkedListType& otherList);
//Function to make a copy of otherList.
//Postcondition: A copy of otherList is created and
// assigned to this list.
};
bool linkedListType::isEmptyList() const
{
return(first == NULL);
}
linkedListType::linkedListType() //default constructor
{
first = NULL;
last = NULL;
count = 0;
}
void linkedListType::destroyList()
{
nodeType *temp; //pointer to deallocate the memory
//occupied by the node
while (first != NULL) //while there are nodes in the list
{
temp = first; //set temp to the current node
first = first->link; //advance first to the next node
delete temp; //deallocate the memory occupied by temp
}
last = NULL; //initialize last to NULL; first has already
//been set to NULL by the while loop
count = 0;
}
void linkedListType::initializeList()
{
destroyList(); //if the list has any nodes, delete them
}
void linkedListType::print() const
{
nodeType *current; //pointer to traverse the list
current = first; //set current so that it points to
//the first node
while (current != NULL) //while more data to print
{
cout << current->info << " ";
current = current->link;
}
}//end print
int linkedListType::length() const
{
return count;
} //end length
int linkedListType::front() const
{
assert(last != NULL);
return first->info; //return the info of the first node
}//end front
int linkedListType::back() const
{
assert(last != NULL);
return last->info; //return the info of the first node
}//end back
bool linkedListType::search(const int& searchItem) const
{
nodeType *current; //pointer to traverse the list
bool found = false;
current = first; //set current to point to the first
//node in the list
while (current != NULL && !found) //search the list
if (current->info == searchItem) //searchItem is found
found = true;
else
current = current->link; //make current point to
//the next node
return found;
}//end search
void linkedListType::insertFirst(const int& newItem)
{
nodeType *newNode; //pointer to create the new node
newNode = new nodeType; //create the new node
assert(newNode != NULL); //if unable to allocate memory,
//terminate the program
newNode->info = newItem; //store the new item in the node
newNode->link = first; //insert newNode before first
first = newNode; //make first point to the
//actual first node
count++; //increment count
if (last == NULL) //if the list was empty, newNode is also
//the last node in the list
last = newNode;
}//end insertFirst
void linkedListType::insertLast(const int& newItem)
{
nodeType *newNode; //pointer to create the new node
newNode = new nodeType; //create the new node
assert(newNode != NULL); //if unable to allocate memory,
//terminate the program
newNode->info = newItem; //store the new item in the node
newNode->link = NULL; //set the link field of newNode
//to NULL
if (first == NULL) //if the list is empty, newNode is
//both the first and last node
{
first = newNode;
last = newNode;
count++; //increment count
}
else //the list is not empty, insert newNode after last
{
last->link = newNode; //insert newNode after last
last = newNode; //make last point to the actual last node
count++; //increment count
}
}//end insertLast
void linkedListType::deleteNode(const int& deleteItem)
{
nodeType *current; //pointer to traverse the list
nodeType *trailCurrent; //pointer just before current
bool found;
if (first == NULL) //Case 1; the list is empty.
cout << "Cannot delete from an empty list."
<< endl;
else
{
if (first->info == deleteItem) //Case 2
{
current = first;
first = first->link;
count--;
if (first == NULL) //the list has only one node
last = NULL;
delete current;
}
else //search the list for the node with the given info
{
found = false;
trailCurrent = first; //set trailCurrent to point
//to the first node
current = first->link; //set current to point to
//the second node
while (current != NULL && !found)
{
if (current->info != deleteItem)
{
trailCurrent = current;
current = current-> link;
}
else
found = true;
}//end while
if (found) //Case 3; if found, delete the node
{
trailCurrent->link = current->link;
count--;
if (last == current) //node to be deleted
//was the last node
last = trailCurrent; //update the value
//of last
delete current; //delete the node from the list
}
else
cout << "The item to be deleted is not in "
<< "the list." << endl;
}//end else
}//end else
}//end deleteNode
void linkedListType::copyList
(const linkedListType& otherList)
{
nodeType *newNode; //pointer to create a node
nodeType *current; //pointer to traverse the list
if (first != NULL) //if the list is nonempty, make it empty
destroyList();
if (otherList.first == NULL) //otherList is empty
{
first = NULL;
last = NULL;
count = 0;
}
else
{
current = otherList.first; //current points to the
//list to be copied
count = otherList.count;
//copy the first node
first = new nodeType; //create the node
assert(first != NULL);
first->info = current->info; //copy the info
first->link = NULL; //set the link field of
//the node to NULL
last = first; //make last point to the
//first node
current = current->link; //make current point to
//the next node
//copy the remaining list
while (current != NULL)
{
newNode = new nodeType; //create a node
assert(newNode != NULL);
newNode->info = current->info; //copy the info
newNode->link = NULL; //set the link of
//newNode to NULL
last->link = newNode; //attach newNode after last
last = newNode; //make last point to
//the actual last node
current = current->link; //make current point
//to the next node
}//end while
}//end else
}//end copyList
linkedListType::~linkedListType() //destructor
{
destroyList();
}//end destructor
linkedListType::linkedListType
(const linkedListType& otherList)
{
first = NULL;
copyList(otherList);
}//end copy constructor
//overload the assignment operator
const linkedListType& linkedListType::operator=
(const linkedListType& otherList)
{
if (this != &otherList) //avoid self-copy
{
copyList(otherList);
}//end else
return *this;
}
i got an error saying error: request for member 'printInfo' in 'current->nodeType::info', which is of non-class type 'int'. There are few more headers file linking to this header, which all have template coding. However i managed to remove template coding from all the other headers with no errors at all with the exception of this header. can anyone tell me how to solve it?
You defined struct nodeType with member int info;. But in addressBookType::printInfoOf (and addressBookType::print) you have the line location->info.printInfo();
Since info is an int, it has no methods to call, printInfo or otherwise, thus the error.
It looks like the problem is a result of another type mismatch in your code. addressBookType's insertNode takes a const extPersonType& (which I assume is defined elsewhere and implements the methods you're trying to use?), but passes it to orderedLinkedListType::insertNode, which takes and stores const int&. This normally wouldn't work, but at a guess, extPersonType is a type (possibly a C++11 strongly-typed enum) that has an implicit conversion to int, which might avoid compile errors during assignment, but still effectively strips all the extPersonType-specific behaviors on insertNode, it goes from extPersonType to just an int.
If you want a general purpose linked list, you need to template it to work with more than just int, e.g. changing nodeType to:
template<typename T>
struct nodeType {
T info;
nodeType<T> *link;
};
and updating all the methods associated with nodeType and the orderedLinkedListType to template as appropriate to the specific type, rather than int. If you're being lazy, you could instead just change all uses of int in nodeType and orderedLinkedListType to extPersonType, but now the code is no good for ints, so pick your poison.

Doubly Linked List : Unresolved Externals [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Why can templates only be implemented in the header file?
What is an undefined reference/unresolved external symbol error and how do I fix it?
Again this is a homework assignment and my instructor has given us plenty of feedback but I am still at a lost for this compile issue.
When I place the main function inside the implementation file the program compiles and works perfectly. However when I place the main function into main.cpp the compiler complains:
unresolved external symbol "public: __thiscall doublyLinkedList<int>::doublyLinkedList<int>(void)" (??0?$doublyLinkedList#H##QAE#XZ) referenced in function
Header File
#ifndef H_doublyLinkedList
#define H_doublyLinkedList
#include <iostream>
#include <cassert>
using namespace std;
//Definition of the node
template <class Type>
struct nodeType
{
Type info;
nodeType<Type> *next;
nodeType<Type> *back;
};
template <class Type>
class doublyLinkedList
{
public:
const doublyLinkedList<Type>& operator=
(const doublyLinkedList<Type> &);
//Overload the assignment operator.
void initializeList();
//Function to initialize the list to an empty state.
//Postcondition: first = NULL; last = NULL; count = 0;
bool isEmptyList() const;
//Function to determine whether the list is empty.
//Postcondition: Returns true if the list is empty,
// otherwise returns false.
void destroy();
//Function to delete all the nodes from the list.
//Postcondition: first = NULL; last = NULL; count = 0;
void print() const;
//Function to output the info contained in each node.
void reversePrint() const;
//Function to output the info contained in each node
//in reverse order.
int length() const;
//Function to return the number of nodes in the list.
//Postcondition: The value of count is returned.
Type front() const;
//Function to return the first element of the list.
//Precondition: The list must exist and must not be empty.
//Postcondition: If the list is empty, the program
// terminates; otherwise, the first
// element of the list is returned.
Type back() const;
//Function to return the last element of the list.
//Precondition: The list must exist and must not be empty.
//Postcondition: If the list is empty, the program
// terminates; otherwise, the last
// element of the list is returned.
bool search(const Type& searchItem) const;
//Function to determine whether searchItem is in the list.
//Postcondition: Returns true if searchItem is found in
// the list, otherwise returns false.
void insert(const Type& insertItem);
//Function to insert insertItem in the list.
//Precondition: If the list is nonempty, it must be in
// order.
//Postcondition: insertItem is inserted at the proper place
// in the list, first points to the first
// node, last points to the last node of the
// new list, and count is incremented by 1.
void deleteNode(const Type& deleteItem);
//Function to delete deleteItem from the list.
//Postcondition: If found, the node containing deleteItem
// is deleted from the list; first points
// to the first node of the new list, last
// points to the last node of the new list,
// and count is decremented by 1; otherwise,
// an appropriate message is printed.
doublyLinkedList();
//default constructor
//Initializes the list to an empty state.
//Postcondition: first = NULL; last = NULL; count = 0;
doublyLinkedList(const doublyLinkedList<Type>& otherList);
//copy constructor
~doublyLinkedList();
//destructor
//Postcondition: The list object is destroyed.
public:
int count;
nodeType<Type> *first; //pointer to the first node
nodeType<Type> *last; //pointer to the last node
public:
void copyList(const doublyLinkedList<Type>& otherList);
//Function to make a copy of otherList.
//Postcondition: A copy of otherList is created and
// assigned to this list.
};
#endif
Implementation file:
#include <iostream>
#include <cassert>
#include "doublyLinkedList.h"
using namespace std;
template <class Type>
doublyLinkedList<Type>::doublyLinkedList()
{
first= NULL;
last = NULL;
count = 0;
}
template <class Type>
bool doublyLinkedList<Type>::isEmptyList() const
{
return (first == NULL);
}
template <class Type>
void doublyLinkedList<Type>::destroy()
{
nodeType<Type> *temp; //pointer to delete the node
while (first != NULL)
{
temp = first;
first = first->next;
delete temp;
}
last = NULL;
count = 0;
}
template <class Type>
void doublyLinkedList<Type>::initializeList()
{
destroy();
}
template <class Type>
int doublyLinkedList<Type>::length() const
{
return count;
}
template <class Type>
void doublyLinkedList<Type>::print() const
{
nodeType<Type> *current; //pointer to traverse the list
current = first; //set current to point to the first node
while (current != NULL)
{
cout << current->info << " "; //output info
current = current->next;
}//end while
}//end print
template <class Type>
void doublyLinkedList<Type>::reversePrint() const
{
nodeType<Type> *current; //pointer to traverse
//the list
current = last; //set current to point to the
//last node
while (current != NULL)
{
cout << current->info << " ";
current = current->back;
}//end while
}//end reversePrint
template <class Type>
bool doublyLinkedList<Type>::
search(const Type& searchItem) const
{
bool found = false;
nodeType<Type> *current; //pointer to traverse the list
current = first;
while (current != NULL && !found)
if (current->info >= searchItem)
found = true;
else
current = current->next;
if (found)
found = (current->info == searchItem); //test for
//equality
return found;
}//end search
template <class Type>
Type doublyLinkedList<Type>::front() const
{
assert(first != NULL);
return first->info;
}
template <class Type>
Type doublyLinkedList<Type>::back() const
{
assert(last != NULL);
return last->info;
}
template <class Type>
void doublyLinkedList<Type>::insert(const Type& insertItem)
{
nodeType<Type> *current; //pointer to traverse the list
nodeType<Type> *trailCurrent; //pointer just before current
nodeType<Type> *newNode; //pointer to create a node
bool found;
newNode = new nodeType<Type>; //create the node
newNode->info = insertItem; //store the new item in the node
newNode->next = NULL;
newNode->back = NULL;
if(first == NULL) //if the list is empty, newNode is
//the only node
{
first = newNode;
last = newNode;
count++;
}
else
{
found = false;
current = first;
while (current != NULL && !found) //search the list
if (current->info >= insertItem)
found = true;
else
{
trailCurrent = current;
current = current->next;
}
if (current == first) //insert newNode before first
{
first->back = newNode;
newNode->next = first;
first = newNode;
count++;
}
else
{
//insert newNode between trailCurrent and current
if (current != NULL)
{
trailCurrent->next = newNode;
newNode->back = trailCurrent;
newNode->next = current;
current->back = newNode;
}
else
{
trailCurrent->next = newNode;
newNode->back = trailCurrent;
last = newNode;
}
count++;
}//end else
}//end else
}//end insert
template <class Type>
void doublyLinkedList<Type>::deleteNode(const Type& deleteItem)
{
nodeType<Type> *current; //pointer to traverse the list
nodeType<Type> *trailCurrent; //pointer just before current
bool found;
if (first == NULL)
cout << "Cannot delete from an empty list." << endl;
else if (first->info == deleteItem) //node to be deleted is
//the first node
{
current = first;
first = first->next;
if (first != NULL)
first->back = NULL;
else
last = NULL;
count--;
delete current;
}
else
{
found = false;
current = first;
while (current != NULL && !found) //search the list
if (current->info >= deleteItem)
found = true;
else
current = current->next;
if (current == NULL)
cout << "The item to be deleted is not in "
<< "the list." << endl;
else if (current->info == deleteItem) //check for
//equality
{
trailCurrent = current->back;
trailCurrent->next = current->next;
if (current->next != NULL)
current->next->back = trailCurrent;
if (current == last)
last = trailCurrent;
count--;
delete current;
}
else
cout << "The item to be deleted is not in list."
<< endl;
}//end else
}//end deleteNode
template <class Type>
void doublyLinkedList<Type>::copyList(const doublyLinkedList<Type>& otherList)
{
//cout << "The definition of this function is left as an exercise." << endl;
//cout << "See Programming Execrise 9." << endl;
template <class Type>
doublyLinkedList<Type>::doublyLinkedList(const doublyLinkedList<Type>& otherList)
{
// cout << "The definition of the copy constructor is left as an exercise." << endl;
// cout << "See Programming Execrise 9." << endl;
}
template <class Type>
const doublyLinkedList<Type>& doublyLinkedList<Type>::operator=
(const doublyLinkedList<Type> &)
// cout << "Overloading the assignment operator is left as an exercise." << endl;
// cout << "See Programming Execrise 9." << endl;
}
template <class Type>
doublyLinkedList<Type>::~doublyLinkedList()
{
//cout << "Definition of the destructor is left as an exercise." << endl;
//cout << "See Programming Execrise 9." << endl;
}
Main Function:
//Program to test various operations on a doubly linked list
#include <iostream>
#include "doublyLinkedList.h"
using namespace std;
int main()
{
char choice;
int n = 0;
doublyLinkedList<int> myList;
cout<<"this is a test"<<endl;
do {
cout<<"Main Menu:"<<endl;
cout<<"Choice of operations to perform on Dobule Linked List"<<endl;
cout<<"Create list values : C"<<endl;
cout<<"Initialize List: Z"<<endl;
cout<<"Check List Empty: M"<<endl;
cout<<"Destroy List: E "<<endl;
cout<<"Print List : P"<<endl;
cout<<"Reverse printed list: R"<<endl;
cout<<"Length of List: L"<<endl;
cout <<"Front of List: F"<<endl;
cout<<"Back of List: B"<<endl;
cout<<"Search list: S"<<endl;
cout<<"Insert List: I"<<endl;
cout<<"delete list: D"<<endl;
cout<<"use copy constructor : U" <<endl;
cout <<"quit: Q"<<endl;
cin >> choice;
if ((choice == 'I' )|| (choice =='D')|| (choice == 'S'))
{
cout<<"Enter value to manipulate: "<<endl;
cin >> n;
}
switch ( choice)
{
case 'C':
cout<<"Please enter a list"<<endl;
while(n!= -999){
myList.insert(n);
cin>>n;}
break;
case 'S': if (myList.search(n))
cout<< " List contains: "<<n<<endl;
else
cout<<"List does not contain "<<n<<endl;
break;
case 'I':
myList.insert(n);
cout<<"element was inserted"<<endl;
break;
case 'D':
myList.deleteNode(n);
cout<<"node was deleted"<<endl;
break;
case 'L': cout<<"Length is \n"<<endl;
myList.length();
break;
case 'B':
cout<<"back element is : "<< myList.back();
break;
case 'F' :
cout<<"front element is "<<myList.front();
break;
case 'Z' : myList.initializeList();
cout<<"list initialized"<<endl;
case 'M': if (myList.isEmptyList())
cout<<"is empty"<< endl;
else
cout<<"is not empty"<<endl;
break;
case 'E': myList.destroy();
cout<<"list destroyed"<<endl;
break;
case 'P': myList.print();
break;
case'R': cout<<"reversed"<<endl;
myList.reversePrint();
break;
}
}while(choice!= 'Q');
return 0;
}
I am looking for guidance. I know the answer is really simple and I am just not seeing it. I thought about using the keyword extern but am not sure really how to use it. Like I said in the tags this is homework so I am not looking for a quick fix I am looking to learn from my mistakes.
I really appreciate this site and all the members.
All the code i posted on here was available for free by the book publisher, I have left out my original code except for main.cpp
You need to move your implementation into the header file. Unlike normal functions the compiler needs to be able to see all of the template code at the point of use.
See the answers in this question for more information:
Why can templates only be implemented in the header file?
The problem is that you don't have the template definitions available in your main compilation unit (where they are used).
This means you need to have explicit instantiations for the types it is used with.
Add
template class doublyLinkedList<int>;
to the end of doublyLinkedList.cpp to explicitely instantiate it.
Or include the cpp in the header,
or directly into main.cpp