Thread: need help w/templates & queues

  1. #1
    Unregistered
    Guest

    need help w/templates & queues

    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

  2. #2
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Posts
    2,138
    You included queue.h in queue.cpp and included queue.h into your main. However, there are no function definitions in queue.h. Instead, include queue.cpp in queue.h and include queue.h in your main file.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. queues
    By rebelfirst in forum C++ Programming
    Replies: 9
    Last Post: 12-02-2007, 05:33 AM
  2. Queues
    By ramayana in forum C Programming
    Replies: 22
    Last Post: 01-01-2006, 02:08 AM
  3. Concatenating two static queues?
    By difficult.name in forum C Programming
    Replies: 2
    Last Post: 10-18-2004, 11:19 PM
  4. queues
    By jamesb in forum C Programming
    Replies: 1
    Last Post: 04-21-2002, 08:57 PM
  5. queues
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 10-11-2001, 05:19 PM