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.
#include <iostream>;
usingnamespace 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:
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&)'