Results 1 to 6 of 6

Thread: [SOLVED] operator<< in c++

  1. #1
    Join Date
    May 2008
    Location
    Sweden
    Beans
    60
    Distro
    Ubuntu 8.10 Intrepid Ibex

    [SOLVED] operator<< in c++

    I'm writing a set class in form of a sorted linked list, in the proccess of making it sort the content of the list. So a good idea would be to print the list on the screen using cout << list << endl;

    I have a header file named setLinkedSorted.h and this is included in main.cpp whereas the functionality of the list is in setLinkedSorted.cpp

    I've written a print function in the setLinkedSorted.cpp file and included it in the header.

    Code:
    void setLinkedSorted::print( ostream & out) const
    {
        Node *tmp = first;
        while( tmp != 0 ) {
          out <<  tmp->value << endl;
          tmp = tmp->next;
        }
    
    
    }
    This function outputs what I want, but it would be nice to use the operator<<

    In the same file I wrote

    Code:
    ostream & setLinkedSorted::operator<< ( const setLinkedSorted & s )
    {
        s.print();
        return cout;
    
    }
    but my output is not what I wanted, it gives me the memory adress 0x804f008 when i use cout << list << endl;

    Anybody got any ideas?
    It's always to soon to give up!

    Problems in Windows? Try "sudo apt-get install ubuntu"

  2. #2
    Join Date
    Jul 2007
    Beans
    44

    Re: operator<< in c++

    why are you sending ostream& out to print
    it should take no parameters and instead of using
    out << tmp->value << endl;
    use
    cout << tmp->value << endl;

    now to overload <<

    use this
    ostream& operator<< ( ostream& out, const setLinkedSorted & s )
    {
    s.print();
    return out;

    }

  3. #3
    Join Date
    Jun 2007
    Location
    Maryland, US
    Beans
    6,274
    Distro
    Kubuntu

    Re: operator<< in c++

    Yeah, you have a missing parameter in your method...

    PHP Code:
    friend std::ostreamoperator<< ( std::ostream &os, const setLinkedSorted ); 
    Then implemented it:
    PHP Code:
    std::ostreamoperator<<( std::ostream &os, const setLinkedSorted &)
    {
      
    Node *tmp s.first;
      while( 
    tmp != ) {
        
    os <<  tmp->value << std::endl;
        
    tmp tmp->next;
      }
      return 
    os;

    Note that this is a friend method; you do not need to add the scope operator ("setLinkedSort::") when implementing the method.
    Last edited by dwhitney67; June 24th, 2008 at 01:23 AM.

  4. #4
    Join Date
    Jun 2007
    Location
    Maryland, US
    Beans
    6,274
    Distro
    Kubuntu

    Re: operator<< in c++

    Quote Originally Posted by tornado89 View Post
    why are you sending ostream& out to print
    it should take no parameters and instead of using
    out << tmp->value << endl;
    use
    cout << tmp->value << endl;

    now to overload <<

    use this
    ostream& operator<< ( ostream& out, const setLinkedSorted & s )
    {
    s.print();
    return out;

    }
    Not a very good implementation if you ask me. What if the user wants to output to a file stream? At a minimum, the print() method should take the ostream parameter, not disregard it.

    Personally, what is the point of having the print() method if the operator<<() method is present?

  5. #5
    Join Date
    May 2008
    Location
    Sweden
    Beans
    60
    Distro
    Ubuntu 8.10 Intrepid Ibex

    Re: operator<< in c++

    Sorry guys, lots of typos.

    Was trying alot of different approaches, and found that I got it to work without the ostream parameter (at least I thought I did)

    The reason for the print method was so that I could skip the friend declaration. Read so in a book, but unfortunatly i did not work.

    Well I got it to work now so thank you guys!
    It's always to soon to give up!

    Problems in Windows? Try "sudo apt-get install ubuntu"

  6. #6
    Join Date
    Jul 2007
    Beans
    44

    Re: [SOLVED] operator&lt;&lt; in c++

    Hey dwhitney67
    I Was Wondering The Same...
    But I Just Wanted To Keep The Code As Close As It Was
    You're Right

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •