简体   繁体   中英

How to overload << cout for a template class?

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 List interface.

template<typename NODETYPE>
ostream &operator <<(ostream& output, const List<NODETYPE>& value)
{ 
   for ( node in value) // pseudocode iteration
   {
     output << node << " ";
   }
   return output;
}

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 by const reference . This has many advantages, one of them being you can pass temporary objects.

Concerning the friend declaration, you also need template<typename T> there, but it isn't clear you need the operator to be friend in the first place. Lists typically provide access to their elements in their public interface.

List<T> 's operator<< should iterate through each element of type T in the list and call output << element; on each.

Then, make sure each type T that you create a List<T> object with also implements an operator<< which outputs each of its variables you would like it to, in the format you would like it to, such as output << "(" << x << "," << y << ")" . Built-in types already do this, so for example List<int> will not require this step.

1) The friend declaration is unnecessary, unless the NODETYPE you want to use as template argument is a class you defined yourself, and the operator<< you are defining wants to access any private members of NODETYPE . It seems that right now, NODETYPE is double , so there is no need for a friend declaration.

If you have other data types you'd like to use as template arguments, and those other data types are your own classes, put a friend declaration inside those classes. (This may be true of the List type, as described below).

2) Right now, your operator<< is recursive. You must (as suggested by the other answers) somehow iterate through the list of NODETYPE objects you get:

for(List::const_iterator it = value.begin() ; it != value.end() ; ++it)
  output << *it;

(The above assumes, your List datatype implements begin() , end() and iterators. You may want to use a different way of iterating through the elements of the List . For that, you may actually have to access private members of the List data type, in which case you must declare the operator<< as a friend template (including typename !) inside the List class definition.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM