Unorderred Array based list

Hey guys i'm very very new to cplus plus and i am creating this data structure for
and array based list class. Im also creating a derived class called unorderedlistType. Unfortunately i have an error in my class unorderedlistType as it cannot detect the protected variables in my base class. Why is that so?

The errors i got is :

In member function 'void unorderredlist<T>::insertEnd(const T&)':
'length' was not declared in this scope
'maxSize' was not declared in this scope
'list' was not declared in this scope




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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237

//   the base and derived class definition ( header file )

#ifndef listType_H
#define listType_H


template <class T>
class listType
{
 
 protected: 
    T *list; // the array list that the object listType will have. It is a pointer because...?
    int maxSize;
	int length; // current size
	
public: 

const listType <T>& operator = (const listType< T > &); // Overloads the assignment operator

bool isEmpty()const;
bool isFull() const;
int getLength () const;
int getmaxSize () const; 
void print () const ;

bool isItemAtEqual (int, const T&) const; 
virtual void insertAt(const T& item, int position);
virtual void insertEnd (const T& insertItem)=0;
void removeAt(int location);
void retrieveAt(int location ,T& retItem) const;
virtual void replaceAt(int location, const T& repItem) = 0;
void clearList();
virtual int seqSearch(const T& searchItem) = 0;
virtual void remove(const T& removeItem) = 0;

listType(int size = 100);
listType(const listType<T>& otherList);

virtual ~listType(); // automatically carry out for the pointer array

};

template <class T>
class unorderredlist:public listType<T>{

public:
void insertAt(const T& item, int position);
void insertEnd (const T& insertItem);
void replaceAt(int location, const T& repItem);
void remove(const T& removeItem);
int seqSearch(const T& searchItem);
unorderredlist(int size = 100);
//~unorderredlist();


};


#endif






//         The Class Method Definition (implementation file)
#include "listType.h"
#include <iostream>

using namespace std;

template <class T>
bool listType<T> :: isEmpty() const  // function to make it Empty
{
	return (length == 0);
}

template <class T>
bool listType<T> :: isFull() const // function to make it Full
{
	return (length == maxSize); 
}

template <class T>
int listType<T> ::getLength() const {
	return length;
}

template <class T>
int listType<T> :: getmaxSize() const {
	return maxSize;
}

template <class T>
void listType<T>::print() const
{
	for(int i = 0; i < length; i++)
		cout << list[i] << " ";
	cout << endl;
}

template <class T>
bool listType<T> :: isItemAtEqual (int location , const T& item) const{ // item is what the user input wants and find if it is equal or not

if(location <0 || location >=length){
	cout<< " The location of the item to be removed is out of range. " << endl;
	return false;
}else
return (list[location]==item);

}

template <class T>
void listType<T>::removeAt(int location)   // Remove an item from list at a specific location
{
	if(location < 0 || location >= length)
		cout << "The location of is out of range." << endl;
	else
	{
		for(int i = location; i < length; i++)
			list[i] = list[i + 1];
		length--;
	}
}

template <class T>
void listType<T>::retrieveAt(int location, T& retItem) const  // make retItem variable outside of this method
{
	if(location < 0 || location >= length)
		cout << "The location of the item to be retrieved is out of range." << endl;
	else
		retItem = list[location];
}

template <class T>
listType<T>::listType(int size)
{
	if(size <= 0)
	{
		cout << "Size must be positive, creating default size of 100." << endl;
		maxSize = 100;
	}
	else
		maxSize = size;
	length = 0;
	list = new T[maxSize];
}

template <class T>
listType<T>::~listType()
{
	delete [] list;
}

template <class T>
const listType<T>& listType<T>:: operator = (const listType<T>& otherList){
	if (this!= &otherList){
		delete[]list;
		maxSize =otherList.maxSize;
		length = otherList.length;
		
		list = new T[maxSize];
		for (int i = 0; i <length; i++){
		list[i]=otherList.list[i];}
	}
	return*this;
}

// UNORDERED LIST TYPE 


template <class T>
void unorderredlist<T>::insertEnd(const T& insertItem){
	
	if (length>=maxSize)
	{
	
	cout<< "Cannot insert in a full list. "<< endl;}
	else{
		list[length]=insertItem;
		length++;
	}
}

template <class T>
int unorderredlist<T>::seqSearch(const T& searchItem){
	int loc;
	bool found =false;
	for (loc =0; loc<length; loc++)
	if (list[loc]==searchItem){
		found=true;
		break;
	}
	if (found)
	return loc;
	else 
	return -1;
}

template <class T>
void unorderredlist<T>::remove(const T& removeItem){
	int loc;
	if(length ==0){
		cout<< "Cannot delete from an empty list. "<< endl;
		
	}else{
		loc= seqSearch(removeItem);
		if(loc!=-1)
		removeAt(loc);
		else
		cout<< "The item to be deleted is not in the list." << endl;
	}
}

template <class T>
void unorderredlist<T>::replaceAt(int location,const T& repItem){

if(location<0 || location >= length)
cout<< "The location of the item to be repleced is out of range. " << endl;
else
list[location]=repItem;
}

template <class T>
unorderredlist<T>::unorderredlist(int size):listType<T>(size){  
}

template <class T>
unorderredlist<T>::~listType()
{
	delete [] list;
}




Last edited on
When deriving from a templated class and the derived class is also templated it cannot directly access the members of the base class. The scope is required. So in your case:

1
2
3
4
5
6
7
8
9
10
11
12
template <class T>
void unorderredlist<T>::insertEnd(const T& insertItem){
	
	if (listType<T>::length>=listType<T>::maxSize)
	{
	
	cout<< "Cannot insert in a full list. "<< endl;}
	else{
		listType<T>::list[listType<T>::length]=insertItem;
		listType<T>::length++;
	}
}
Thanks so much. It now works.
But one more thing why is it that i can't declare the unorderredlist to be size of whatever i want. like this code below:

#include "List Definition.h"
#include <iostream>
using namespace std;

int main(){

unorderredlist<string> stringList(50);



}

error:
(.rdata$_ZTV8listTypeISsE[_ZTV8listTypeISsE]+0x10): undefined reference to `listType<std::string>::insertAt(std::string const&, int)'



below is my updated insertAt function code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
template<class T>
void unorderredlist<T>::insertAt(const T& item, int location)
{
	if(location < 0 || location >= unorderredlist<T>::maxSize)
		cout<<"The index is out of range."<<endl;
	else if(unorderredlist<T>::length >= unorderredlist<T>::maxSize)
		cout<<"The list is full."<<endl;
	else
	{
		for(int i = unorderredlist<T>::length; i > location; i--)
			unorderredlist<T>::list[i] = unorderredlist<T>::list[i - 1];
		unorderredlist<T>::list[location] = item;
		unorderredlist<T>::length++;
	}
}
Last edited on
The compiler needs to see the entire definition of an templated function when it is used. I.e. Within a .cpp (which is not include) the templated function is ignored and not seen when use elsewhere. Hence you need to put the entire definition into the header (or other file) which is #include'd
oh i manage to fix it. i forgot to put =0; at the virtual function insertAt() in function definition.
Topic archived. No new replies allowed.