Inheritance variable undeclared

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);
};
Last edited on
The error is clear: It is telling you that line #22 is using something called 'first' but it hasn't been declared. Where did you declare 'first'? Same reasoning for lines 49, 52, 107 & 122.

For now correct this and some of the other errors will go away as are consequences of the first ones.

Also note that Dev-C++ is an abandoned project (or so I hear). Its compiler should be sooo outdated by now that it would be a miracle if programs executed properly in newer OS's. Switch!

EDIT: All the come down to type, then up to check thing led me to stop reading the entire post. I just read that you declared them in an ancestor class. Since they are protected, they should be available. Could it be the old compiler? Don't really know. Maybe. But in the meantime see if qualifying the variables help (as in linkedListType<Type>::first).
Last edited on
Dear webJose,

First of all, thanks a lot for informing me that Dev-c++ is outdated. I never knew that! Our lecturer asked us to use dev-c++ as our standard compiler though.

Next, I seriously hope its not due to my old compiler! And, I do not understand what you mean by qualifying the variables? Where and how do I do it? As in using linkedListType<Type>::first where? hmm...
Please explain further.
Thanks.

Thanks a lot for going through this post.
I really really appreciate it!

Best regards,
Kiong
In line 22, where it says current = first;, try current = linkedListType<Type>::first;. That's qualifying.

Yes, last I heard, dev-C++ is years outdated. Basically it is close to worthless now. Try Code::Blocks, or a similar to dev-C++, wxDev-C++. If you are in Windows, my 100% all-time favorite is Visual Studio by Microsoft. But granted, it is bigger and meaner and maybe it is not for the absolute beginner, I would say.
Last edited on
I just tried qualifying the variables. It did not come out any more errors. Do you know why?

And there was another issue which I forgot to mention in my replies above which is there was an error with count in the derived class. I have declared count as int in the ancestor class though. (The error that came out was "no post increment for type)

Hmm...

Visual Studio? hmm.
I heard about Visual Studio, Visual C++ and visual basic. what are the differences?
Visual Studio is an IDE that contains compilers and tools for multiple languages, like C++/visual basic/.NET languages, etc
You could use it for only C/C++.
Topic archived. No new replies allowed.