I want to override the<<(output) operator.

Below, cout<<* (currentEntry->getValue ());I want to display like this,
He said he needed to override<<.
I don't know how to override it, so I'm doing trial and error.
Could you give me some advice?

The error is like this.

template<typename Type>// Added this.
class Animal {
    private: private:
        int id;
        int name
        template<typename Type>// I wrote the typename Type above this function.
        void operator<<(HashEntry<Type>* tmp) {cout<<tmp->getValue ();}

The error that appears when added above is the following error.

using namespace std;
template<typename Type>
class HashEntry {
    private: private:
        int key;
        Type value;
        HashEntry * next;
// I want to use the<<operator that I overridden below.
void printTable () {
    HashEntry<Type>* currentEntry = table [i];
    while (currentEntry->getNext ()! = NULL) {
        // cout<<* (currentEntry->getValue ());I don't think this should be used.
  • Answer # 1

    I want to override the<<(output) operator.

    Stream output operator for your own class templateoperator<<When providing overload, it will be described in one of the following two patterns. In either pattern, the return value and the first argument arestd :: ostream&Should be.

    template<typename Type>
    class MyClass {
      // ...
      // (1) Overloaded as a friend function in the class definition
      friend std :: ostream&operator<<(std :: ostream&os, const MyClass&self) {
        os<<self./*...*/;// Output member variables (private can also be accessed)
        return os;// Always return the output stream os
    // (2) Overload as a free function separately from the class definition
    template<typename Type>
    std :: ostream&operator<<(std :: ostream&os, const MyClass<Type>&self) {
      os<<self./*...*/;// Output member variables (only public can access)
      return os;// Always return the output stream os

    *: Wide characters (wchar_t) Also if you want to supportstd :: basic_ostreamYou need to define an operator overload for. For simplicity in this answerstd :: ostreamOnly targeted.

  • Answer # 2

    To overload ostream operator<<, you can do the following.
    Do not define operator<


    // Sample class
    struct Abc {
        int a;
    // Output the contents of class Abc
    // operator<<
    std :: ostream&operator<<(std :: ostream&os, Abc&a) {

  • Answer # 3

    The error message is as follows.TypeDoes not exist in the scope.

    I can't say for sure because I don't understand the intention, but "TypeRewrite to a concrete type "or"class AnimalItselftemplateI think that either "template with" is what you want to do.