i am trying to convert a queue class to a template. I keep getting errors from my operator overloading. can anyone tell me what i'm doing wrong?
//queue.h
#ifndef _QUEUE_H
#define _QUEUE_H
template <class T>
class Queue {
friend ostream& operator << (ostream &, Queue &);
public:
T dequeue(); //removes element from front
void enqueue(T); // adds element to back
bool empty();
T front (); //returns element from front
Queue(); //constructor
private:
void incr (int &); //increment index of array
T myData[1300]; // array of data
int myNextFreePosition; // next free position
int myFront; //front of queue
};
#endif
//queue.cpp
#include "queue.h"
Queue::Queue() {
myNextFreePostion = 0;
myFront = 0;
}
void Queue:enqueue (T item) {
myData[myNextFreePostion] = item;
incr (myNextFreePosition);
}
T Queue::dequeue() {
T retval = myData [myFront];
return retval;
}
T Queue::front() {
return myData[myFront];
}
bool Queue::empty() {
return (myNextFreePostion == myFront);
}
ostream& operator << (ostream& out, Queue q) {
out << "Front" << endl;
for (int i = q.myFront; i != q.myNextFreePosition; q.incr(i)) {
out << q.myData[i] << ", ";
}
out << "END" << endl;
return out;
}
}
//main.cpp
#include "queue.h"
#include <iostream.h>
int main() {
Queue <int> q;
q.enqueue(123);
q.enqueue(42);
cout << q;
q.dequeue();
cout << q;
}
here are some of the errors i'm getting.
queue.h(9) : error C2143: syntax error : missing ';' before '&'
queue.h(23) : see reference to class template instantiation 'Queue<T>' being compiled
queue.h(9) : error C2433: 'ostream' : 'friend' not permitted on data declarations
queue.h(23) : see reference to class template instantiation 'Queue<T>' being compiled
queue.h(9) : error C2501: 'ostream' : missing storage-class or type specifiers
queue.h(23) : see reference to class template instantiation 'Queue<T>' being compiled
queue.h(9) : error C2244: 'ostream' : unable to resolve function overload
queue.h(23) : see reference to class template instantiation 'Queue<T>' being compiled
queue.h(9) : error C2061: syntax error : identifier 'ostream'
queue.h(23) : see reference to class template instantiation 'Queue<T>' being compiled
queue.h(9) : error C2501: '<<' : missing storage-class or type specifiers
queue.h(23) : see reference to class template instantiation 'Queue<T>' being compiled
queue.h(9) : error C2805: binary 'operator <<' has too few parameters
queue.h(23) : see reference to class template instantiation 'Queue<T>' being compiled