Problems Using an Overloaded stream operator as a friend

I'm currently writing a templated queue class and I am running into some issues with overloading the << stream operator. When I go to compile it in gcc, I get the following errors.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
queue.h:65: warning: friend declaration `std::ostream& operator<<(std::ostream&, queue<T>&)' declares a non-template function
queue.h:65: warning: (if this is not what you intended, make sure the function template has already been declared and add <> after the function name here) -Wno-non-template-friend disables this warning
In file included from queue.h:76,
                 from queueApp.cpp:8:
queue.cpp: In function `std::ostream& operator<<(std::ostream&, queue<T>&)':
queue.cpp:164: error: `node' was not declared in this scope
queue.cpp:164: error: `nodePtr' was not declared in this scope
queue.cpp:166: error: `front' was not declared in this scope
queue.h:65: warning: friend declaration `std::ostream& operator<<(std::ostream&, queue<T>&)' declares a non-template function
queue.h:65: warning: (if this is not what you intended, make sure the function template has already been declared and add <> after the function name here) -Wno-non-template-friend disables this warning
In file included from queue.h:76:
queue.cpp: In function `std::ostream& operator<<(std::ostream&, queue<T>&)':
queue.cpp:164: error: `node' was not declared in this scope
queue.cpp:164: error: `nodePtr' was not declared in this scope
queue.cpp:166: error: `front' was not declared in this scope


I feel like I am trying to use the overloaded stream in a way that it was not meant to be used. However, I'm not sure what the proper way to it would be.

queue.h
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
#include <iostream>
using namespace std;

#ifndef QUEUE_H
#define QUEUE_H

template <class T>
class queue
{

	private:
		
		// The class for storing all the node information
		class node
		{
			public:
				node() {next = NULL;}
				node(T value)	{data = value; next = NULL;}
				T data;
				node *next;
		};

		// Attributes
		node *front;
		node *rear;

	public:

		// Constructor
		queue()		{front=NULL;rear=NULL;}

		// The Big Three
		// The Copy Constructor
		queue(const queue &someQueue);
		// The Assignment Operator
		queue& operator=(const queue &rhs);
		// The Destuctor that deletes all the nodes in the queue
		~queue();
		
		// Returns true if the queue is empty
		// otherwise, false
		bool isEmpty();

		// Removes all the items in the queue
		void clear();

		// Adds an item to the queue
		void enqueue(T item);

		// Removes an item from the queue and passes it by reference
		bool dequeue(T &item);

		// Returns the number of elements in the queue
		int size();

		// Overloading the output stream operator to allow the application
		// programmer to display the queue on the screen.
		friend ostream& operator <<(ostream &output, queue<T> &rhs);

};

/*
// Overloading the output stream operator to allow the application
// programmer to display the queue on the screen.
template <class T>
ostream &operator<<(ostream &output, queue<T> &rhs);
*/

#include "queue.cpp"

#endif



queue.cpp
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
//#ifndef QUEUE_CPP
//#define QUEUE_CPP

#include "queue.h" 

// The Big Three

// The Copy Constructor
template <class T>
queue<T>::queue(const queue &someQueue)
{

	node *nodePtr; // Temporary Pointer
	
	if (this != &someQueue)
	{
		clear();
		nodePtr = someQueue.front;

		while(nodePtr != NULL)
		{
			enqueue(nodePtr->data);
			nodePtr = nodePtr->next;
		}
	}

}

// The Assignment Operator
template <class T>
queue<T> &queue<T>::operator=(const queue &rhs)
{

	node *nodePtr; // Temporary Pointer
	
	if (this != &rhs)
	{
		clear();
		nodePtr = rhs.front;

		while(nodePtr != NULL)
		{
			enqueue(nodePtr->data);
			nodePtr = nodePtr->next;
		}
	}

	return *this;

}

// The Destuctor that deletes all the nodes in the queue
template <class T>
queue<T>::~queue()
{
	clear();
}


// Member Functions

// Returns true if the queue is empty
// otherwise, false
template <class T>
bool queue<T>::isEmpty()
{
	if (front == NULL)
		return true;
	else
		return false;
}

// Removes all the items in the queue
template <class T>
void queue<T>::clear()
{

	T item; // Temporary Pointer

	while( !isEmpty() )
		dequeue(item);

}

// Adds an item to the queue
template <class T>
void queue<T>::enqueue(T item)
{

	if( isEmpty() )
	{
		front = new node(item);
		rear = front;
	}
	else
	{
		rear->next = new node(item);
		rear = rear->next;
	}

}

// Removes an item from the queue and passes it by reference
template <class T>
bool queue<T>::dequeue(T &item)
{

	node *nodePtr;

	if ( !isEmpty() ) // If the list is empty, don't bother doing anything
	{
		item = front->data;
		nodePtr = front;
		front = front->next;
		
		if( front == NULL )
			rear = NULL;

		delete nodePtr;
		return true;

	}

	return false;

}

// Returns the number of elements in the queue
template <class T>
int queue<T>::size()
{
	
	int size = 0;

	node *nodePtr; // For traversing the queue

	nodePtr = front;

	while( nodePtr != NULL)
	{
		size++;
		nodePtr = nodePtr->next;
	}

	return size;

}


// Stream Operators

// Overloading the output stream operator to allow the application
// programmer to display the queue on the screen.
template <class T>
ostream &operator<<(ostream &output, queue<T> &rhs)
{
	node *nodePtr;

	nodePtr = front;
	
	while (nodePtr != NULL)
	{
		output << nodePtr->data << endl;
		nodePtr = nodePtr->next;
	}

	return output;
}

//#endif 
Last edited on
The easiest solution is to implement the ostream operator directly in the class declaration itself. You have to
move the entire contents of queue.cpp to the header anyway otherwise you'll end up with linker errors.

I had considered that; however, I've always been taught to write my class prototypes and member functions in different files. It seems (to me at least) to be easier to read and pick out problems in individual function.
Except that with templates, you don't have that option. You must put the definitions of the template functions/classes inside the header file, because the compiler must have access to the implementations of said functions/classes at the point of invocation/usage (ie, other .cpp files).
Topic archived. No new replies allowed.