do not understand the errors

There are errors in this code that im having, i believe i have narrowed it down to a node of the variable Type pointing to the value int, there is no conversion for this. BUT according to my teacher this is how the code works and my error is somewhere else in my code.

The purpose of the program is to take the function count and make it return the number of elements in the queue using nodes in some complicated way.

header file
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
//Header file QueueAsArray

#ifndef H_HEADER
#define H_HEADER
  
#include <iostream>
#include <cassert>

using namespace std;

template <class Type>
struct nodeType
{
    Type info;
    nodeType<Type> *link;
};

template <class Type>
class queueType
{
public:
    const queueType<Type>& operator=(const queueType<Type>&); 
            // overload the assignment operator
    void initializeQueue();
    int isEmptyQueue() const;
    int isFullQueue() const;

    Type front() const;

    Type back() const;
	int queueCount();
    void addQueue(const Type& newElement);
    void deleteQueue();

    queueType(int queueSize = 100); 
    queueType(const queueType<Type>& otherQueue); // copy constructor
    ~queueType(); //destructor

private:
    int maxQueueSize;
    int queueFront;
    int queueRear;
	int count;
    Type *list;  //pointer to array that holds queue elements 
};


template<class Type>
int queueType<Type>::queueCount()
{
    return count;
}

template<class Type>
void queueType<Type>::initializeQueue()
{
    nodeType<Type> *temp;
 
    while (queueFront!= NULL)   //while there are elements left
                                //in the queue
    {
        temp = queueFront;    //set temp to point to the current node
        queueFront = queueFront->link;  //advance first to the
                                                       //next node
        delete temp;       //deallocate memory occupied by temp
    }
    queueRear = NULL;  //set rear to NULL
    count = 0;
} //end initializeQueue

template<class Type>
int queueType<Type>::isEmptyQueue() const
{
    return (queueFront == queueRear);
}

template<class Type>
int queueType<Type>::isFullQueue() const
{
    return ((queueRear + 1) % maxQueueSize == queueFront);
}

template <class Type>
void queueType<Type>::addQueue(const Type& newElement)
{
       nodeType<Type> *newNode;
 
       newNode = new nodeType<Type>;  //create the node
 
       newNode->info = newElement;    //store the info
    newNode->link = NULL;         //initialize the link field to NULL
      
       if (queueFront == NULL)          //if initially the queue is empty
    {
          queueFront = newNode;
          queueRear = newNode;
    }
    else                          //add newNode at the end
    {
          queueRear->link = newNode;
          queueRear = queueRear->link;
    }
       count++;
}//end addQueue

template<class Type>
void queueType<Type>::deleteQueue()
{
   nodeType<Type> *temp;
  
   if (!isEmptyQueue())
   {
	   temp = queueFront;       //make temp point to the first node
	   queueFront = queueFront->link; //advance queueFront
	   delete temp;                  //delete the first node
	   
	   if (queueFront == NULL)   //if after deletion the queue is empty
	   {
		   queueRear = NULL;    //set queueRear to NULL
		   count--;
	   }
   }
   else
cout << "Cannot remove from an empty queue." << endl;
}//end deleteQueue

template<class Type>
Type queueType<Type>::front() const
{
       assert(queueFront != NULL);
       return queueFront->info;
}

template<class Type>
Type queueType<Type>::back() const
{
    assert(!isEmptyQueue());
    return list[(queueRear + 1) % maxQueueSize];
}

template<class Type>
queueType<Type>::queueType(int queueSize)   //constructor
{
    if (queueSize <= 0)
    {
        cout << "Size of the array to hold the queue must "
             << "be positive."<<endl;
        cout << "Creating an array of size 100." << endl;

        maxQueueSize = 100;
    }
    else
        maxQueueSize = queueSize;  //set maxQueueSize to queueSize

    queueFront = maxQueueSize - 1;  //initialize queueFront
    queueRear = maxQueueSize - 1;   //initiaize queueRear
    list = new Type[maxQueueSize];  //create the array to
				                    //hold queue elements
}

template<class Type>
queueType<Type>::~queueType()   //destructor
{
    delete [] list;
}

template<class Type>
const queueType<Type>& queueType<Type>::operator=(const queueType<Type>& otherQueue)
{
    int j;

    if (this != &otherQueue) //avoid self-copy
    {
        maxQueueSize = otherQueue.maxQueueSize;
        queueFront = otherQueue.queueFront;
        queueRear = otherQueue.queueRear;	

        delete [] list;
        list = new Type[maxQueueSize];

        if (queueFront != queueRear)						//if other queue is not empty
        for (j = (queueFront + 1) % maxQueueSize; j <= queueRear; 
                                j = (j + 1) % maxQueueSize)  //copy other queue in this queue
            list[j] = otherQueue.list[j];
    } //end if

    return *this;
}

template<class Type>
queueType<Type>::queueType(const queueType<Type>& otherQueue)
{
	maxQueueSize = otherQueue.maxQueueSize;
	queueFront = otherQueue.queueFront;
	queueRear = otherQueue.queueRear;

	list = new Type[maxQueueSize];

	if (queueFront != queueRear)	//if other queue is not empty
		for (int j = (queueFront + 1) % maxQueueSize; j <= queueRear; 
								   j = (j + 1) % maxQueueSize)  //copy other queue in this queue
			list[j] = otherQueue.list[j];
	
}

#endif 


main file
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
#include <iostream>
#include "header.h"

using namespace std;
 
void testCopyConstructor(queueType<int> otherQueue);
 
int main()
{
	queueType<int> queue1, queue2;
	int x, y;

	x = 4;
	y = 5;
	queue1.addQueue(x);
	queue1.addQueue(y);
	x = queue1.front();
	queue1.deleteQueue();
	queue1.addQueue(x + 5);
	queue1.addQueue(16);
	queue1.addQueue(x);
	queue1.addQueue(y - 3);
	queue1.addQueue(343);

	testCopyConstructor(queue1);	

	queue2 = queue1;

	queue2.addQueue(222);

	cout << "queue1: ";

	while (!queue1.isEmptyQueue())
	{
		cout << queue1.front() << " ";
		queue1.deleteQueue();
	}

	cout << endl << "the number of elements in queue 1 is " << queue1.queueCount() << endl;

	cout << "queue2: ";

	while (!queue2.isEmptyQueue())
	{
		cout << queue2.front() << " ";
		queue2.deleteQueue();
	}

	cout << endl << "the number of elements in queue 2 is " << queue2.queueCount() << endl;

	cin.get();
	return 0;
}

void testCopyConstructor(queueType<int> otherQueue)
{
	if (!otherQueue.isEmptyQueue())
	{
		cout << "Other Queue is not empty" << endl;
		cout << "Front element of Other Queue : " 
			 << otherQueue.front() << endl;
		otherQueue.deleteQueue();
	}
}
And where the error message?

I get this errors:
header.h
1) line 59 while (queueFront!= NULL) //while there are elements left
warning is "NULL used in arithmetic".

2) line 63 queueFront = queueFront->link; //advance first to the
queueFront->link is not a pointer. So it do not have "->link". It is "int queueFront;"

3) line 93 if (queueFront == NULL) //if initially the queue is empty
warning is "NULL used in arithmetic".

4) line 100 queueRear->link = newNode;
queueRear->link is not a pointer. So it do not have "->link". It is int queueRear;"

And here are more... like this.
Last edited on
okay let me rephrase my question, my teacher has the code the way she wants it, and i have to figure ways around these errors but how do you go about fixing errors?
When I fix errors I often start fixing the error that is reported first. Later errors could be caused by earlier errors so often it's enough fixing the first error and most of the other errors will be gone.
As Shinigami hint you confuse indices with pointers.

Since queueType does not contain a variable that can hold nodeType<Type> i don't see why you do as if.

So remove all (remain) references to nodeType and treat queueFront/queueRear as what they are: indices into an array of 'Type'

Looking at back() front() would be:
1
2
3
4
5
6
template<class Type>
Type queueType<Type>::front() const
{
       assert(!isEmptyQueue());
       return list[queueFront]; // I don't see why % is necessary?
}
Topic archived. No new replies allowed.