Good morning everyone!
I need a little assistance in overloading cout for my linked list. The truth is, I have no idea how to do it. I tried searching everywhere but no where explains it to the depth that I need it.
This is an assignment (I've already written the whole program but I can't output anything without overloading cout). Also, I have more than one object to output, and the iteration required... I'm at a loss.
My lecturer said "Write an assignment operator and a friend function to output the linked list"
I've got 3 source files. A ListNode.h, list.h and main.cpp... In my list.h file, I put:
1 2
|
template <typename OUTPUT >
friend ostream &operator <<(ostream &, const List<NODETYPE> & );
|
before this, I had <typename NODETYPE> but I get an error about shadows param, which is probably because it's in my template class.
My questions are:
Where exactly do I put this code? I have it before my public and private functions in my class before all my function prototypes. Is this the right location for it?
Also, how do I define it: In my main.cpp (that I'm not allowed to alter in any way as this is what the lecturer provided for us) I have these objects (though they may differ..he just provided examples so we could test if the functions all do what they're supposed to):
1 2
|
List<int> Li, Li2, Li3;
List<double> Ld, Ld2;
|
and I also have several cout statements like:
cout << "Ld is: " << Ld << endl;
I've tried defining the overload statement like this but it doesn't work:
1 2 3 4 5 6
|
template<typename NODETYPE>
ostream &operator <<(ostream &output, const List<NODETYPE>& list)
{
output << list;
return output;
}
|
Any help is greatly appreciated!! Thanks =]