After my main program runs, I encounter several errors which I could not rectify or identify the source of its error(the reason behind it as well). I would be hoping you guys would be kind enough to help me out.
My compiler is dev-c++
The errors are:
In member function 'bool orderedLinkedList<Type>::search(const Type&)const':
line 22 : 'first' undeclared[each undeclared identifier is reported only once for each function it appears in]
In member function 'void orderedLinkedList<Type>::insert(const Type&)":
line 49 'first' undeclared.
line 52 'last' undeclared.
line 53,75 & 83 no post-increment operator for type
In member function 'void orderedLinkedList<Type>::deleteNode(const Type&)':
line 107 'first' undeclared
line 122 'last' undeclared
line 143 no post-decrement operator for type
In main program:
line 5 expected primary-expression before "int"
expected ";" before "int"
line 34 expected "}" at end of input
end of errors.
Question : For the undeclared variables, I have already put that the orderedLinkedList class is an inheritance to class linkedListType-first and last is a protected variable here. So, I do not really understand where I went wrong.
Most baffling to me is the error in the main program about expecting primary expression before "int"-refers to the int main()....
-------------------------------------------------------------------------------
//Main Program
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
|
#include <iostream>
#include "orderedLinkedList.h"
using namespace std;
int main()
{
orderedLinkedList<int> list1, list2;
int num;
cout<<"\n Enter any number (0 will stop the loop) : ";
cin>>num;
while(num!=0)
{
list1.insert(num);
cin>>num;
}
cout<<"\n\n list1 : ";
list1.print();
list2=list1;
cout<<"\n list2 : ";
list2.print();
cout<<"\n Enter the number to be deleted : ";
cin>>num;
list2.deleteNode(num);
cout<<"\n After deleting "<<num<<", list2 : ";
list2.print();
cout<<"\n\n";
system("PAUSE");
return 0;
}
|
-------------------------------------------------------------------------------
//orderedLinkedList - to sort the input data in order
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 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
|
#include <iostream>
#include "linkedListType.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 &newItem);
void deleteNode(const Type &deleteItem);
};
template<class Type>
bool orderedLinkedList<Type>::search(const Type &searchItem)const
{
bool found = false;
nodeType<Type> *current;
current = first;
while(current!=NULL)
{
if(current->info >= searchItem)
found = true;
else
current = current->link;
}
if(found)
found = (current->info == searchItem);
return found;
}
template<class Type>
void orderedLinkedList<Type>::insert(const Type &newItem)
{
nodeType<Type> *current;
nodeType<Type> *trailCurrent;
nodeType<Type> *newNode;
bool found;
newNode = new nodeType<Type>;
newNode->info = newItem;
newNode->link = NULL;
if(first == NULL)
{
first = newNode;
last = newNode;
count++;
}
else
{
current = first;
found = false;
while(current != NULL && !found)
{
if(current->info >= newItem)
found = true;
else
{
trailCurrent = current;
current = current->link;
}
}
if(current == first)
{
newNode->link = first;
first = newNode;
count++;
}
else
{
trailCurrent->link = newNode;
newNode->link = current;
if(current == NULL)
last = newNode;
count++;
}
}
}
template<class Type>
void orderedLinkedList<Type>::insertFirst(const Type &newItem)
{
insert(newItem);
}
template<class Type>
void orderedLinkedList<Type>::insertLast(const Type &newItem)
{
insert(newItem);
}
template<class Type>
void orderedLinkedList<Type>::deleteNode(const Type &deleteItem)
{
nodeType<Type> *current;
nodeType<Type> *trailCurrent;
bool found;
if (first == NULL)
cout<<" Unable to delete when there is no data \n";
else
{
current = first;
found = false;
while (current != NULL && !found)
{
if(current->info >= deleteItem)
found = true;
else
{
trailCurrent = current;
current = current->link;
}
}
if(current == NULL)
cout<<" That data set is not available. Sorry, please try again\n";
else
{
if(current->info == deleteItem)
{
if(first == current)
{
first = first->link;
if(first == NULL)
last = NULL;
delete current;
}
else
{
trailCurrent->link = current->link;
if(current == last)
last = trailCurrent;
delete current;
}
count--;
}
else
cout<<" That data set is not available. Sorry, please try again\n";
}
}
|
-------------------------------------------------------------------------------
//a generic function to process linkedList - as an ordered or unordered linked list
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
|
#include <iostream>
#include "linkedListIterator.h"
using namespace std;
template<class Type>
class linkedListType
{
public:
const linkedListType<Type> &operator=(const linkedListType<Type>&);
void initializeList();
bool isEmptyList()const;
void print() const;
int length() const;
void destroyList();
Type front() const;
Type back() const;
virtual bool search(const Type &searchItem) const=0;
virtual void insertFirst(const Type &newItem) =0;
virtual void insertLast (const Type &newItem) =0;
virtual void deleteNode (const Type &deleteItem) =0;
linkedListIterator<Type> begin();
linkedListIterator<Type> end();
linkedListType();
linkedListType(const linkedListType<Type> &otherList);
~linkedListType();
protected:
int count;
nodeType<Type> *first;
nodeType<Type> *last;
private:
void copyList(const linkedListType<Type> &otherList);
};
|