By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,652 Members | 1,727 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,652 IT Pros & Developers. It's quick & easy.

ostream

P: n/a
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::operator <<(int value) {
crd->output(0,(double)value);
return *this;
}

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

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

OutputStream& OutputStream::operator <<(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::operator<< <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

Aug 28 '06 #1
Share this Question
Share on Google+
6 Replies


P: n/a

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::operator <<(int value) {
crd->output(0,(double)value);
return *this;
}

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

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

OutputStream& OutputStream::operator <<(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::operator<< <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::operator<<(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::operator<<(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

Aug 28 '06 #2

P: n/a
On 28 Aug 2006 03:09:42 -0700 in comp.lang.c++, "silversurfer2025"
<tk********@googlemail.comwrote,
>
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"

Aug 28 '06 #3

P: n/a
"silversurfer2025" <tk********@googlemail.comwrote in message
news:11**********************@i3g2000cwc.googlegro ups.com...
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::ostream doesn't make its << operators virtual, so this almost certainly
won't work. If you have:

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

then this function will *always* call std::ostream::operator<<(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::ostream? 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::operator<< <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::ostream &outref = out;
outref << 4;

it would call std::ostream::operator<<(int) as I mentioned above, because
outref is a std::ostream& 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::ostream seems to create more problems than it solves.

Philip

Aug 28 '06 #4

P: n/a
Philip Potter schrieb:
"silversurfer2025" <tk********@googlemail.comwrote in message
news:11**********************@i3g2000cwc.googlegro ups.com...
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::ostream doesn't make its << operators virtual, so this almost certainly
won't work. If you have:

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

then this function will *always* call std::ostream::operator<<(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::ostream? 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::operator<< <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::ostream &outref = out;
outref << 4;

it would call std::ostream::operator<<(int) as I mentioned above, because
outref is a std::ostream& 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::ostream seems to create more problems than it solves.
Thanks once more, I will try to use streambuf

Greetings and thanks once more
Tim

Aug 28 '06 #5

P: n/a
"silversurfer2025" <tk********@googlemail.comwrote in message
news:11*********************@74g2000cwt.googlegrou ps.com...
Philip Potter schrieb:
"silversurfer2025" <tk********@googlemail.comwrote in message
news:11**********************@i3g2000cwc.googlegro ups.com...
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::ostream? 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::operator<<(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'

Aug 28 '06 #6

P: n/a

Philip Potter wrote:
"silversurfer2025" <tk********@googlemail.comwrote in message
news:11**********************@i3g2000cwc.googlegro ups.com...
///////////////////////////////////////////////////////////////////////////
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::ostream? 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

Aug 28 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.