ostream Problem

Discussion in 'C++' started by Mike Copeland, May 1, 2014.

  1. I am trying to combine std::left and std::setw() as a single
    function. The following code ignores the "left" manipulator. What am I
    doing wrong? TIA

    struct leftsetw // combined functions (left/width)
    {
    leftsetw(int w) : width(w) {}
    int width;
    };
    ostream &operator<<(ostream &o, const leftsetw &a)
    {
    o.left, o.width(a.width);
    return o;
    }

    ostringstream oss;
    oss.str(""); oss << "Test " << leftsetw(12) << 42;

    ---
    This email is free from viruses and malware because avast! Antivirus protection is active.
    http://www.avast.com
     
    Mike Copeland, May 1, 2014
    #1
    1. Advertisements

  2. On 5/1/2014 4:46 PM, Mike Copeland wrote:
    > I am trying to combine std::left and std::setw() as a single
    > function. The following code ignores the "left" manipulator. What am I
    > doing wrong? TIA
    >
    > struct leftsetw // combined functions (left/width)
    > {
    > leftsetw(int w) : width(w) {}
    > int width;
    > };
    > ostream &operator<<(ostream &o, const leftsetw &a)
    > {
    > o.left, o.width(a.width);


    Try calling the function instead of evaluating the function pointer:

    o.left(),

    > return o;
    > }
    >
    > ostringstream oss;
    > oss.str(""); oss << "Test " << leftsetw(12) << 42;


    V
    --
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, May 1, 2014
    #2
    1. Advertisements

  3. Mike Copeland

    Stefan Ram Guest

    (Mike Copeland) writes:
    >o.left, o.width(a.width);


    #include <iostream>
    #include <sstream>

    struct leftsetw
    { int const width;
    leftsetw( int const width ): width{ width } {} };

    ::std::eek:stream & operator<<
    ( ::std::eek:stream & ostream, const leftsetw & leftsetw )
    { ostream.width( leftsetw.width );
    ostream.setf( ::std::ios_base::left, std::ios_base::adjustfield );
    return ostream; }

    int main()
    { ::std::eek:stringstream ostringstream;
    ostringstream << '|' << leftsetw( 2 )<< 3 << '|';
    ::std::cout << ostringstream.str() << '\n'; }
     
    Stefan Ram, May 1, 2014
    #3
  4. Mike Copeland

    Stefan Ram Guest

    Victor Bazarov <> writes:
    >Try calling the function instead of evaluating the function pointer:
    >o.left(),


    ::std::left( o )

    N3797, 27.5.6.2
     
    Stefan Ram, May 1, 2014
    #4
    1. Advertisements

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.
Similar Threads
  1. Alex Dribin

    Line wrapping in ostream

    Alex Dribin, Aug 6, 2003, in forum: C++
    Replies:
    2
    Views:
    603
    Dietmar Kuehl
    Aug 6, 2003
  2. Parity
    Replies:
    2
    Views:
    594
    Stephen Howe
    Aug 13, 2003
  3. joesoap

    ostream operator

    joesoap, Aug 20, 2003, in forum: C++
    Replies:
    1
    Views:
    677
    Victor Bazarov
    Aug 20, 2003
  4. Ryan M. Keith
    Replies:
    0
    Views:
    511
    Ryan M. Keith
    Oct 16, 2003
  5. Ryan M. Keith
    Replies:
    0
    Views:
    445
    Ryan M. Keith
    Oct 16, 2003
  6. keit6736
    Replies:
    2
    Views:
    468
    Sergiy Kanilo
    Oct 17, 2003
  7. Johannes Barop
    Replies:
    1
    Views:
    898
    JH Trauntvein
    Dec 29, 2005
  8. Replies:
    2
    Views:
    2,292
Loading...