ostream

S

silversurfer2025

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
 
S

silversurfer2025

silversurfer2025 said:
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
 
D

David Harmon

On 28 Aug 2006 03:09:42 -0700 in comp.lang.c++, "silversurfer2025"
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"
 
P

Philip Potter

silversurfer2025 said:
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
 
S

silversurfer2025

Philip said:
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!
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).
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
 
P

Philip Potter

silversurfer2025 said:
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'
 
B

BigBrian

Philip said:
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
 

Ask a Question

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. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,037
Messages
2,570,371
Members
47,014
Latest member
TashaMorei

Latest Threads

Top