Overloading the output operator << as a friend

Ok, so I've searched, I've googled, I've tried a lot of different things but nothing ends up working for me. My assignment states that I am to overload the output operator as a friend function for a given Queue implementation. We did it in an earlier assignment for Stacks, but that was as a member function - not friend. Needless to say I've hit the wall and need some help declaring and defining the << operator as a friend of class QueType.

The 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
#include <iostream>;
using namespace std;

template <class ItemType>
struct NodeType;

template <class ItemType>
class QueType
{
    
public:
    QueType();
    // Class constructor.
   ~QueType();
    // Class destructor.
    void MakeEmpty();
    // Function: Initializes the queue to an empty state.
    // Post: Queue is empty.
    bool IsEmpty() const;
    // Function: Determines whether the queue is empty.
    // Post: Function value = (queue is empty)
    bool IsFull() const;
    // Function: Determines whether the queue is full.
    // Post: Function value = (queue is full)
    void AddQ(ItemType newItem);
    // Function: Adds newItem to the rear of the queue.
    // Pre:  Queue is not full.
    // Post: newItem is at the rear of the queue.
    void RemoveQ(ItemType& item);
    // Function: Removes front item from the queue and returns it in item.
    // Pre:  Queue is not empty.
    // Post: Front element has been removed from the queue.
    //       item is a copy of the removed element
    QueType(const QueType<ItemType>& orig);
    QueType<ItemType> operator=(const QueType<ItemType> &q);
    int size();
    friend ostream& operator<<( ostream& out, const QueType<ItemType> &q);

private:
    NodeType<ItemType>* qFront;
    NodeType<ItemType>* qRear;
};


And the definition I have from the implementation file:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
ostream& operator<<( ostream& out,const QueType<ItemType> &q)
{
    NodeType<ItemType>* tempPtr = q.qFront;
    if(tempPtr == NULL)
        out << "Queue is empty." << endl;
    else
    {
         while(tempPtr != NULL)
         {
           out << tempPtr->info << " ";
           tempPtr = tempPtr->next;
         }

         out << endl;
    }

}


If someone could just help me out with the proper way to declare and begin the definition, I would be very very grateful.

Thanks!
I'd say...
1
2
template <typename ItemType>
ostream& operator<<( ostream& out,const QueType<ItemType> &q)
Oops I forgot to add that when I posted my definition.

I have...
1
2
template <class ItemType>
ostream& operator<<( ostream& out,const QueType<ItemType> &q)


Anyways, my error seems to be when I try to output a Queue. For example when I try to write...
1
2
3
4
5
6
QueType<int> intQue; //create a queue of ints

for(int i = 1; i <=5; i++) //fill the queue with 1 2 3 4 5
    intQue.AddQ(i);

cout << intQue;  //output the queue  


I get errors from the compiler saying
1
2
3
4
5
6
Queue2.h:46: warning: friend declaration `std::ostream& operator<<(std::ostream&, const QueType<ItemType>&)' declares a non-template function

and

main.cpp:44: undefined reference to `operator<<(std::basic_ostream<char, std::char_traits<char> >&, QueType<int> const&)'



Topic archived. No new replies allowed.