Operation Overloading

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • n355a
    New Member
    • Oct 2006
    • 4

    Operation Overloading

    How would I overload the cout (<< ) operator in this function???

    template <class T>
    void Queue<T>::Print Q()
    {

    cout << “The content of the array is as follows: ” << endl;

    for (int i=0; i< theQ.size(); i++)

    cout << theQ[i] << endl;
    }


    i tried to follow an example and came up with this... but i don't think this is right...

    template <class T>
    void Queue<T>::Print Q()
    {

    cout << “The content of the array is as follows: ” << endl;

    }



    ostream& operator<<(ostr eam& os, PrintQ& theQ)
    {
    for (int i=0; i< theQ.size(); i++)

    cout << theQ[i] << end;
    return os;
    }

    please help... THANKS!
  • m013690
    New Member
    • Sep 2006
    • 23

    #2
    In the function parameters list you listed the function name PrintQ. That should actually be a type (int, char, or in your case Queue). Then follow it with & theQ. Also, if you want to be able to use 'cout' in the normal way ( cout << theQ[c] ), your overloaded function cannot be a member function of the queue object, so unless the items you are printing are public members, the overloaded '<<' operator will not have direct access to the items being printed.

    In general, though, your function is almost right on as it. Just change that one thing, and it looks fine.

    Oh, and I saw one more thing. you said 'theQ[i]' but the queue object must have an array somewhere in it. It should be theQ.arrayName[i]. Your code would try to output the contents of theQ at index i, but that won't work unless theQ is an array itself, OR you have also overloaded the [ ] operator to work that way.

    Comment

    • n355a
      New Member
      • Oct 2006
      • 4

      #3
      Thanks for the help but when i call the PrintQ function how does it know to also call the ostream& operator<< function???


      template <class T>
      void Queue<T>::Print Q()
      {

      cout << “The content of the array is as follows: ” << endl;

      }



      ostream& operator<<(ostr eam& os, PrintQ& theQ)
      {
      for (int i=0; i< theQ.size(); i++)

      cout << theQ[i] << end;
      return os;
      }

      Comment

      • m013690
        New Member
        • Sep 2006
        • 23

        #4
        Originally posted by n355a
        Thanks for the help but when i call the PrintQ function how does it know to also call the ostream& operator<< function???


        template <class T>
        void Queue<T>::Print Q()
        {

        cout << “The content of the array is as follows: ” << endl;

        }
        by overloading the '<<' function, you're teaching the compiler how to output an entire Queue object to the screen. So, somewhere in your printQ function, you just need this line:

        cout << theQ;

        When the compiler sees this, it generates the following function call:

        operator<< ( cout, theQ );

        Inside the overloaded '<<' is where you put all the hard work of walking through the arrays of the Queue to write out individual elements which are of some base type (int, char, whatever).

        That help?

        Comment

        Working...