ostream

Discussion in 'C++' started by silversurfer2025, Aug 28, 2006.

  1. Hello,

    I am currently trying to derive a class from ostream (which is giving
    output to my GUI), such that I can give my methods either std::cout or
    my own outputstream-class to be used as output stream.

    Unfortunately, I get a runtime error: "segmentation fault" each time I
    am using the overloaded << function but cannot find the mistakt.. Maybe
    somebody hwo is more of n expert than me (which shouldn't be too hard)
    knows what to change?

    ///////////////////////////////////////////////////////////////////////////
    My header file:

    #ifndef _outputstream_
    #define _outputstream_

    #include <iostream>
    #include "FrameWork.h"

    class OutputStream : public ostream{

    public:
    FrameWork* crd;
    OutputStream(FrameWork* crd2);
    OutputStream& operator <<(int value);
    OutputStream& operator <<(double value);
    OutputStream& operator <<(char* value);
    OutputStream& operator <<(std::string value);
    };

    #endif /*_outputstream_*/


    ///////////////////////////////////////////////////////////////////////////
    And here is my definition-file .cpp

    #include "OutputStream.h"


    OutputStream::OutputStream(FrameWork* crd2) {
    crd=crd2;
    }

    OutputStream& OutputStream::eek:perator <<(int value) {
    crd->output(0,(double)value);
    return *this;
    }

    OutputStream& OutputStream::eek:perator <<(double value) {
    crd->output(0,value);
    return *this;
    }

    OutputStream& OutputStream::eek:perator <<(char* value) {
    std::string tmp(value);
    crd->output(0,value);
    return *this;
    }

    OutputStream& OutputStream::eek:perator <<(std::string value) {
    crd->output(0,value);
    return *this;
    }
    ///////////////////////////////////////////////////////////////////////////

    The call in the framework-program is:

    OutputStream out(this);
    out << 4 << "Willkommen!";

    and the error I get:
    #0 0x0efad884 in std::eek:perator<< <std::char_traits<char> > () from
    /usr/lib/libstdc++.so.6
    #1 0x1003b0bc in FeatureExtraction::showFeatureConfig
    (this=0x10135b40, os=@0x10131830) at ../FeatureExtraction.cpp:243
    //this is where I call the << method.


    Thanks a lot in advance, I have been trying around for quite some time
    now and am looking forward to what you have to say to it.

    Greetings
    Tim
     
    silversurfer2025, Aug 28, 2006
    #1
    1. Advertising

  2. silversurfer2025 schrieb:

    > Hello,
    >
    > I am currently trying to derive a class from ostream (which is giving
    > output to my GUI), such that I can give my methods either std::cout or
    > my own outputstream-class to be used as output stream.
    >
    > Unfortunately, I get a runtime error: "segmentation fault" each time I
    > am using the overloaded << function but cannot find the mistakt.. Maybe
    > somebody hwo is more of n expert than me (which shouldn't be too hard)
    > knows what to change?
    >
    > ///////////////////////////////////////////////////////////////////////////
    > My header file:
    >
    > #ifndef _outputstream_
    > #define _outputstream_
    >
    > #include <iostream>
    > #include "FrameWork.h"
    >
    > class OutputStream : public ostream{
    >
    > public:
    > FrameWork* crd;
    > OutputStream(FrameWork* crd2);
    > OutputStream& operator <<(int value);
    > OutputStream& operator <<(double value);
    > OutputStream& operator <<(char* value);
    > OutputStream& operator <<(std::string value);
    > };
    >
    > #endif /*_outputstream_*/
    >
    >
    > ///////////////////////////////////////////////////////////////////////////
    > And here is my definition-file .cpp
    >
    > #include "OutputStream.h"
    >
    >
    > OutputStream::OutputStream(FrameWork* crd2) {
    > crd=crd2;
    > }
    >
    > OutputStream& OutputStream::eek:perator <<(int value) {
    > crd->output(0,(double)value);
    > return *this;
    > }
    >
    > OutputStream& OutputStream::eek:perator <<(double value) {
    > crd->output(0,value);
    > return *this;
    > }
    >
    > OutputStream& OutputStream::eek:perator <<(char* value) {
    > std::string tmp(value);
    > crd->output(0,value);
    > return *this;
    > }
    >
    > OutputStream& OutputStream::eek:perator <<(std::string value) {
    > crd->output(0,value);
    > return *this;
    > }
    > ///////////////////////////////////////////////////////////////////////////
    >
    > The call in the framework-program is:
    >
    > OutputStream out(this);
    > out << 4 << "Willkommen!";
    >
    > and the error I get:
    > #0 0x0efad884 in std::eek:perator<< <std::char_traits<char> > () from
    > /usr/lib/libstdc++.so.6
    > #1 0x1003b0bc in FeatureExtraction::showFeatureConfig
    > (this=0x10135b40, os=@0x10131830) at ../FeatureExtraction.cpp:243
    > //this is where I call the << method.
    >
    >
    > Thanks a lot in advance, I have been trying around for quite some time
    > now and am looking forward to what you have to say to it.
    >
    > Greetings
    > Tim


    OK, I have written the problem in a way which might be misleading. Here
    another try: When using OutputStream as return parameter in the
    methods, I get a compiler error (with the same outputstream code) from
    the line
    > out << 4 << "Willkommen!";

    telling me that :
    error: ISO C++ says that these are ambiguous, even though the worst
    conversion for the first is better than the worst conversion for the
    second:
    .../OutputStream.h:22: note: candidate 1: OutputStream&
    OutputStream::eek:perator<<(char*)
    /usr/lib/gcc/powerpc-linux-gnu/4.0.3/../../../../include/c++/4.0.3/bits/ostream.tcc:616:
    note: candidate 2: std::basic_ostream<char, _Traits>&
    std::eek:perator<<(std::basic_ostream<char, _Traits>&, const char*) [with
    _Traits = std::char_traits<char>]

    However, if I change the return type to be ostream& then I get a
    segmentation fault on runtime..

    Anybody knows a solution to this?

    Again, looking forward to your suggestions,
    Tim
     
    silversurfer2025, Aug 28, 2006
    #2
    1. Advertising

  3. silversurfer2025

    David Harmon Guest

    On 28 Aug 2006 03:09:42 -0700 in comp.lang.c++, "silversurfer2025"
    <> wrote,
    >
    >I am currently trying to derive a class from ostream (which is giving
    >output to my GUI), such that I can give my methods either std::cout or
    >my own outputstream-class to be used as output stream.


    This is almost always the wrong approach. Derive your class from
    streambuf, and create a stream using it.

    Google for "streambuf"
     
    David Harmon, Aug 28, 2006
    #3
  4. "silversurfer2025" <> wrote in message
    news:...
    > I am currently trying to derive a class from ostream (which is giving
    > output to my GUI), such that I can give my methods either std::cout or
    > my own outputstream-class to be used as output stream.


    std::eek:stream doesn't make its << operators virtual, so this almost certainly
    won't work. If you have:

    void func(std::eek:stream &foo) {
    foo << 42;
    }

    then this function will *always* call std::eek:stream::eek:perator<<(int), even if
    foo is actually referring to an OutputStream which defines its own
    operator<<(int).

    >

    ///////////////////////////////////////////////////////////////////////////
    > My header file:
    >
    > #ifndef _outputstream_
    > #define _outputstream_
    >
    > #include <iostream>
    > #include "FrameWork.h"
    >
    > class OutputStream : public ostream{


    This shouldn't compile since there is no class "ostream" in scope. Did you
    want std::eek:stream? Is your compiler nonconforming? Or did you post code
    which wasn't the same as the code you compiled?

    > The call in the framework-program is:
    >
    > OutputStream out(this);
    > out << 4 << "Willkommen!";
    >
    > and the error I get:
    > #0 0x0efad884 in std::eek:perator<< <std::char_traits<char> > () from
    > /usr/lib/libstdc++.so.6
    > #1 0x1003b0bc in FeatureExtraction::showFeatureConfig
    > (this=0x10135b40, os=@0x10131830) at ../FeatureExtraction.cpp:243
    > //this is where I call the << method.


    out << 4;

    This will call your operator<<(int) method. This is fine - but only because
    out explicitly has OutputStream type. If you did this:

    std::eek:stream &outref = out;
    outref << 4;

    it would call std::eek:stream::eek:perator<<(int) as I mentioned above, because
    outref is a std::eek:stream& and its operator<<(int) is nonvirtual.

    Finally, the segfault itself:

    out << "Willkommen!";

    This looks for operator<<(char *) - NOT std::string. It can't find it in
    your class, to it calls the ostream's one instead. Unfortunately, you
    haven't set up the base ostream properly, so all bets are off.

    I suggest you try a different approach. Deriving your class from
    std::eek:stream seems to create more problems than it solves.

    Philip
     
    Philip Potter, Aug 28, 2006
    #4
  5. Philip Potter schrieb:

    > "silversurfer2025" <> wrote in message
    > news:...
    > > I am currently trying to derive a class from ostream (which is giving
    > > output to my GUI), such that I can give my methods either std::cout or
    > > my own outputstream-class to be used as output stream.

    >
    > std::eek:stream doesn't make its << operators virtual, so this almost certainly
    > won't work. If you have:
    >
    > void func(std::eek:stream &foo) {
    > foo << 42;
    > }
    >
    > then this function will *always* call std::eek:stream::eek:perator<<(int), even if
    > foo is actually referring to an OutputStream which defines its own
    > operator<<(int).

    shoot... OK, thanks for the tip!

    >
    > >

    > ///////////////////////////////////////////////////////////////////////////
    > > My header file:
    > >
    > > #ifndef _outputstream_
    > > #define _outputstream_
    > >
    > > #include <iostream>
    > > #include "FrameWork.h"
    > >
    > > class OutputStream : public ostream{

    >
    > This shouldn't compile since there is no class "ostream" in scope. Did you
    > want std::eek:stream? Is your compiler nonconforming? Or did you post code
    > which wasn't the same as the code you compiled?

    Hm.. the compiler did not produce errors and this is the original code
    I posted. I am using g++ (gcc).

    >
    > > The call in the framework-program is:
    > >
    > > OutputStream out(this);
    > > out << 4 << "Willkommen!";
    > >
    > > and the error I get:
    > > #0 0x0efad884 in std::eek:perator<< <std::char_traits<char> > () from
    > > /usr/lib/libstdc++.so.6
    > > #1 0x1003b0bc in FeatureExtraction::showFeatureConfig
    > > (this=0x10135b40, os=@0x10131830) at ../FeatureExtraction.cpp:243
    > > //this is where I call the << method.

    >
    > out << 4;
    >
    > This will call your operator<<(int) method. This is fine - but only because
    > out explicitly has OutputStream type. If you did this:
    >
    > std::eek:stream &outref = out;
    > outref << 4;
    >
    > it would call std::eek:stream::eek:perator<<(int) as I mentioned above, because
    > outref is a std::eek:stream& and its operator<<(int) is nonvirtual.
    >
    > Finally, the segfault itself:
    >
    > out << "Willkommen!";
    >
    > This looks for operator<<(char *) - NOT std::string. It can't find it in
    > your class, to it calls the ostream's one instead. Unfortunately, you
    > haven't set up the base ostream properly, so all bets are off.
    >
    > I suggest you try a different approach. Deriving your class from
    > std::eek:stream seems to create more problems than it solves.

    Thanks once more, I will try to use streambuf

    Greetings and thanks once more
    Tim
     
    silversurfer2025, Aug 28, 2006
    #5
  6. "silversurfer2025" <> wrote in message
    news:...
    > Philip Potter schrieb:
    >
    > > "silversurfer2025" <> wrote in message
    > > news:...
    > > > My header file:
    > > >
    > > > #ifndef _outputstream_
    > > > #define _outputstream_
    > > >
    > > > #include <iostream>
    > > > #include "FrameWork.h"
    > > >
    > > > class OutputStream : public ostream{

    > >
    > > This shouldn't compile since there is no class "ostream" in scope. Did

    you
    > > want std::eek:stream? Is your compiler nonconforming? Or did you post code
    > > which wasn't the same as the code you compiled?

    > Hm.. the compiler did not produce errors and this is the original code
    > I posted. I am using g++ (gcc).


    I tried to compile your code (with g++, even!). You don't provide
    FrameWork.h (you should provide minimal complete compilable code -
    commenting out all code dependent on FrameWork would have been wise). After
    commenting out that #include, I got the list of errors at the end of this
    post. Note that the very first error complains about the line I talked
    about.

    What you've most likely done is written "using namespace std;" at some point
    in FrameWork.h or one of the files which it includes. This is bad. See FAQ
    27.5.

    (It's even worse than the FAQ makes out, because you've put the 'using'
    directive in a header file; any file which #includes this header has /no
    choice/ but to include the whole std namespace in its code.)

    Philip

    In file included from OutputStream.cpp:1:
    OutputStream.h:7: syntax error before `{' token
    OutputStream.h:11: syntax error before `*' token
    OutputStream.h:12: `OutputStream& operator<<(int)' must have an argument of
    class or enumerated type
    OutputStream.h:12: `OutputStream& operator<<(int)' must take exactly two
    arguments
    OutputStream.h:13: `OutputStream& operator<<(double)' must have an argument
    of
    class or enumerated type
    OutputStream.h:13: `OutputStream& operator<<(double)' must take exactly two
    arguments
    OutputStream.h:14: `OutputStream& operator<<(char*)' must have an argument
    of
    class or enumerated type
    OutputStream.h:14: `OutputStream& operator<<(char*)' must take exactly two
    arguments
    OutputStream.h:15: `OutputStream& operator<<(std::basic_string<char,
    std::char_traits<char>, std::allocator<char> >)' must take exactly two
    arguments
    OutputStream.h:16: syntax error before `}' token
    OutputStream.cpp:4: `FrameWork' was not declared in this scope
    OutputStream.cpp:4: `crd2' was not declared in this scope
    OutputStream.cpp:4: invalid use of undefined type `class OutputStream'
    OutputStream.h:7: forward declaration of `class OutputStream'
    OutputStream.cpp:4: invalid declarator
    OutputStream.cpp:4: syntax error before `{' token
    OutputStream.cpp:8: invalid use of undefined type `class OutputStream'
    OutputStream.h:7: forward declaration of `class OutputStream'
    OutputStream.cpp: In member function `OutputStream&
    OutputStream::eek:perator<<(int)':
    OutputStream.cpp:9: `crd' undeclared (first use this function)
    OutputStream.cpp:9: (Each undeclared identifier is reported only once for
    each
    function it appears in.)
    OutputStream.cpp: At global scope:
    OutputStream.cpp:13: invalid use of undefined type `class OutputStream'
    OutputStream.h:7: forward declaration of `class OutputStream'
    OutputStream.cpp:18: invalid use of undefined type `class OutputStream'
    OutputStream.h:7: forward declaration of `class OutputStream'
    OutputStream.cpp:24: invalid use of undefined type `class OutputStream'
    OutputStream.h:7: forward declaration of `class OutputStream'
     
    Philip Potter, Aug 28, 2006
    #6
  7. silversurfer2025

    BigBrian Guest

    Philip Potter wrote:
    > "silversurfer2025" <> wrote in message
    > news:...
    > >

    > ///////////////////////////////////////////////////////////////////////////
    > > My header file:
    > >
    > > #ifndef _outputstream_
    > > #define _outputstream_
    > >
    > > #include <iostream>
    > > #include "FrameWork.h"
    > >
    > > class OutputStream : public ostream{

    >
    > This shouldn't compile since there is no class "ostream" in scope. Did you
    > want std::eek:stream? Is your compiler nonconforming? Or did you post code
    > which wasn't the same as the code you compiled?


    Or there's a

    using namespace std;

    in FrameWork.h
     
    BigBrian, Aug 28, 2006
    #7
    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. Alex Dribin

    Line wrapping in ostream

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

    ostream operator

    joesoap, Aug 20, 2003, in forum: C++
    Replies:
    1
    Views:
    420
    Victor Bazarov
    Aug 20, 2003
  4. Victor Irzak

    ABC ostream & operator <<

    Victor Irzak, Sep 20, 2003, in forum: C++
    Replies:
    3
    Views:
    483
    Jerry Coffin
    Sep 21, 2003
  5. Replies:
    2
    Views:
    1,911
Loading...

Share This Page