I've done some searching and tinkered with syntax, and it comes down to, I have no idea how to do this. Currently the function in question is a mess because I've spent some time just throwing things at it to see if anything sticks.
I've got a template linked-list style queue class and I need to overload <<. I want to be able to do things with it like say:
cout << aQueue;
or
outfile << aQueue;
and have it display all elements in the queue. I kind of need to overload the << operator instead of just returning a string, because I'm going to need to have queues of user-defined objects and work with their own << overloads.
#ifndef Queue_h
#define Queue_h
#include <iostream>
usingnamespace std;
template <class Data>
class Queue{
private:
struct Node {
Data content;
Node* next;
};
int length;
Node* first;
Node* last;
public:
Queue();
//Default constructor, creates a new Queue object.
bool IsFull() const;
// Function: Determines whether queue is full.
bool IsEmpty() const;
//Determines whether queue is empty
int GetLength() const;
// Returns number of elements in queue
void RemoveFront();
//removes item from front of queue without returning anything
Data GetFront();
//returns the first item in the queue, removes first item in queue
Data ViewFront();
//Returns the first item in the queue without removing it.
void InsertItem(Data item);
// Function: Adds item to queue.
void MakeEmpty();
//Empties Queue
ostream operator<<(Queue<Data> theQueue);
//Display all items in queue.
~Queue();
//Destructor
};
#endif
#include "Queue.cpp"
I get the error:
error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'Queue<Data>' (or there is no acceptable conversion)...
I have no idea if I'm at all on the right track for what I want to do or if I should be starting again from scratch on this function.
I'm equally confused on how to do this if I make it a friend function instead.
I poked around a bit more and got this working. Since questions in this theme seem to come up quite a bit, here's what I was missing:
Even though the friend function is declared inside the class in the .h file, unlike the actual member functions it needs to be explicitly declared to be a template function.
@Paul2
Yes, you have it. It is not a member function, it is a friend, meaning you must also specify all the template stuff in the friend declaration prototype.
@SIK
No, don't do that. You should not ever #include .cpp files.
For templates, you have to include the .cpp file in the header (or simply not have a cpp file). When you #include "Queue.h" in another .cpp file, and create for example a Queue<int> object, the compiler needs to generate definitions for all of the functions in Queue<int>. Without the definitions of these functions available in Queue.h, the compiler would be unable to do this. In this case, the OP is including the implementation at the very bottom of Queue.h.
Point taken Duoas - putting template logic in a .cpp file is bad practice and also consequently will lead to the bad practice of needing to include those .cpp files.
Was just pointing out to Paul2 to include .cpp to get his project to compile from the position he was at.