How to return stdout from a method ?

Discussion in 'C++' started by barthelemy.von.haller@gmail.com, Jun 30, 2008.

  1. Guest

    Hi,

    I am trying to return the standard output from a method like that :

    ostream *XXX::GetDebugStream() {
    // return debugFile; // I don't want that, but it works
    // return stdout; // this returns : error: cannot convert
    `_IO_FILE*' to `std::eek:stream*' in return
    // return cout; // this returns : invalid conversion from
    `void*' to `std::eek:stream*'
    }

    I tried to return stdout or cout but it doesn't work. I understand why
    it doesn't work with cout but not with stdout.

    Could someone explain me what I am doing wrong ?

    Thanks in advance

    Barth
     
    , Jun 30, 2008
    #1
    1. Advertising

  2. Daniel Kraft Guest

    wrote:
    > Hi,
    >
    > I am trying to return the standard output from a method like that :
    >
    > ostream *XXX::GetDebugStream() {
    > // return debugFile; // I don't want that, but it works
    > // return stdout; // this returns : error: cannot convert
    > `_IO_FILE*' to `std::eek:stream*' in return
    > // return cout; // this returns : invalid conversion from
    > `void*' to `std::eek:stream*'
    > }
    >
    > I tried to return stdout or cout but it doesn't work. I understand why
    > it doesn't work with cout but not with stdout.


    try
    return &cout;

    cout is not a pointer, thus you will have to take the address of it; and
    stdout is not a C++ ostream class but rather a FILE* you can use with
    fprintf and friends.

    Daniel

    > Could someone explain me what I am doing wrong ?
    >
    > Thanks in advance
    >
    > Barth



    --
    Done: Bar-Sam-Val-Wiz, Dwa-Elf-Hum-Orc, Cha-Law, Fem-Mal
    Underway: Ran-Gno-Neu-Fem
    To go: Arc-Cav-Hea-Kni-Mon-Pri-Rog-Tou
     
    Daniel Kraft, Jun 30, 2008
    #2
    1. Advertising

  3. Guest

    On 30 juin, 12:51, Daniel Kraft <> wrote:
    > wrote:
    > > Hi,

    >
    > > I am trying to return the standard output from a method like that :

    >
    > > ostream *XXX::GetDebugStream() {
    > > // return debugFile; // I don't want that, but it works
    > > // return stdout; // this returns : error: cannot convert
    > > `_IO_FILE*' to `std::eek:stream*' in return
    > > // return cout; // this returns : invalid conversion from
    > > `void*' to `std::eek:stream*'
    > > }

    >
    > > I tried to return stdout or cout but it doesn't work. I understand why
    > > it doesn't work with cout but not with stdout.

    >
    > try
    > return &cout;
    >
    > cout is not a pointer, thus you will have to take the address of it; and
    > stdout is not a C++ ostream class but rather a FILE* you can use with
    > fprintf and friends.
    >
    > Daniel
    >
    > > Could someone explain me what I am doing wrong ?

    >
    > > Thanks in advance

    >
    > > Barth

    >
    > --
    > Done: Bar-Sam-Val-Wiz, Dwa-Elf-Hum-Orc, Cha-Law, Fem-Mal
    > Underway: Ran-Gno-Neu-Fem
    > To go: Arc-Cav-Hea-Kni-Mon-Pri-Rog-Tou


    Thank you very much, it works. I don't know why I was so sure that
    cout was a pointer...

    Barth
     
    , Jun 30, 2008
    #3
  4. On Jun 30, 8:29 am, wrote:
    > On 30 juin, 12:51, Daniel Kraft <> wrote:
    >
    >
    >
    > > wrote:
    > > > Hi,

    >
    > > > I am trying to return the standard output from a method like that :

    >
    > > > ostream *XXX::GetDebugStream() {
    > > > // return debugFile; // I don't want that, but it works
    > > > // return stdout; // this returns : error: cannot convert
    > > > `_IO_FILE*' to `std::eek:stream*' in return
    > > > // return cout; // this returns : invalid conversion from
    > > > `void*' to `std::eek:stream*'
    > > > }

    >
    > > > I tried to return stdout or cout but it doesn't work. I understand why
    > > > it doesn't work with cout but not with stdout.

    >
    > > try
    > > return &cout;

    >
    > > cout is not a pointer, thus you will have to take the address of it; and
    > > stdout is not a C++ ostream class but rather a FILE* you can use with
    > > fprintf and friends.

    >
    > > Daniel

    >
    > > > Could someone explain me what I am doing wrong ?

    >
    > > > Thanks in advance

    >
    > > > Barth

    >
    > > --
    > > Done: Bar-Sam-Val-Wiz, Dwa-Elf-Hum-Orc, Cha-Law, Fem-Mal
    > > Underway: Ran-Gno-Neu-Fem
    > > To go: Arc-Cav-Hea-Kni-Mon-Pri-Rog-Tou

    >
    > Thank you very much, it works. I don't know why I was so sure that
    > cout was a pointer...
    >
    > Barth


    Did you tried returning the reference, instead of a pointer?
    Just like the << operator?

    std::eek:stream& getDebugStream()
    {
    return std::cout;
    }
     
    Darío Griffo, Jun 30, 2008
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Elad
    Replies:
    0
    Views:
    422
  2. Greenhorn
    Replies:
    15
    Views:
    849
    Keith Thompson
    Mar 6, 2005
  3. Xeno Campanoli
    Replies:
    0
    Views:
    238
    Xeno Campanoli
    Feb 13, 2006
  4. Andreas S
    Replies:
    3
    Views:
    274
    Eric Hodel
    Dec 9, 2006
  5. Replies:
    2
    Views:
    354
    A. Sinan Unur
    Dec 7, 2005
Loading...

Share This Page