We were taught how to overload cout the other day for our program to cout but I don’t know how to make it output everything.
template <NODETYPE>
friend ostream &operator <<(ostream &, List<NODETYPE>& );
template<typename NODETYPE>
ostream &operator <<(ostream& output, List<NODETYPE>& value)
{
output << value;
return output;
}
However, my program has at least 5 objects to output and two of them are doubles. I get an error for that that says ‘double is not a valid type for a template constant parameter’
My two problems are: How do I output all my objects and not just the first object; and how do I get the double to output. Please and thanks!
EDIT: HUGE EDIT:::
Okay, I realized I was doing something wrong, rearranged my header, and source files.
And then I also realized that missing my lecturer’s class was one of the biggest mistakes I’ve ever made. My next error, was giving you all my assumptions, and not the information that I assumed from.
In my assignment, it says: • Write an assignment operator and a friend function to output the linked list.
in almost every other line of my main function(a function that I’m not allowed to alter), there is a cout:
List<int> Li, Li2, Li3;
List<double> Ld, Ld2;
These are my objects. And all my couts look something like this:
cout << "Ld is: " << Ld << endl;
After rearranging my header and source files, I got this error:
“no match for ‘operator<<‘ in ‘std::operator<<[with_Traits = std::char_traits] (((std::basic_ostream>&)(& std::cout)), ((const char*) ‘Ld is”))<
I get that for every single cout statement I have. It’s more information than Ld exit status is 1 or whatever, so I’m going from this.
I’m still not fully keen on using this ostream overload function, so any help appreciated and thank you so much for your time!
EDIT::–
I’ve put almost all my code in this post: collect2: Ld returned 1 exit status build make error
If someone could help me with the overload that’d be great, because I think it’s the only problem I’ve got left so I can figure out everything else.
Thanks!!
You need to perform some kind of iteration over the
List<NODETYPE>, printing out each node. Otherwise you have an infinite recursion, with the operator calling itself.This example prints out the elements separated by a single space, in a single line. I have omitted the details of the iteration mechanism because I don’t know your
Listinterface.This assumes there is an
ostream& operator<<for the node types, if not you have to provide that too. Also, note I pass the list byconst reference. This has many advantages, one of them being you can pass temporary objects.Concerning the
frienddeclaration, you also needtemplate<typename T>there, but it isn’t clear you need the operator to befriendin the first place. Lists typically provide access to their elements in their public interface.