473,386 Members | 1,958 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

ostream& as parameter -> segfault? / understanding polymorphia

My class looks very exactly like this (left out boring things):

class Situation {
public:
// ...
virtual void toStream(std::ostream& ostr) const;
};

std::string Situation::toString() const {
std::ostringstream oss;
toStream(oss);
return oss.str();
}

std::ostream& operator<<(std::ostream& ostr, const Situation& s) {
s.toStream(ostr); // segfault here
return ostr;
}

The toStream()-Method shall describe the class instance by writing some
things to the given stream.

Additionally I have lots of classes which derive from Situation and have
their own version of toStream().

The idea now is I have a set of References to these dervied classes (but
I hold them in a Situation& as I dont know the exact class of each one),
and I'd like to output them to cout or whatever.

But when I do I get a segfault on calling toStream()?
Why?

As the second parameter to oeprator<< is a reference I should be able to
take advantage of polymorphic behaviour, or not?

TIA
Henning

Jun 23 '06 #1
2 2716
Henning Hasemann wrote:
My class looks very exactly like this (left out boring things):

class Situation {
public:
// ...
virtual void toStream(std::ostream& ostr) const;
};

std::string Situation::toString() const {
std::ostringstream oss;
toStream(oss);
return oss.str();
}

std::ostream& operator<<(std::ostream& ostr, const Situation& s) {
s.toStream(ostr); // segfault here
return ostr;
}

The toStream()-Method shall describe the class instance by writing some
things to the given stream.

Additionally I have lots of classes which derive from Situation and have
their own version of toStream().

The idea now is I have a set of References to these dervied classes (but
I hold them in a Situation& as I dont know the exact class of each one),
and I'd like to output them to cout or whatever.

But when I do I get a segfault on calling toStream()?
Why?

As the second parameter to oeprator<< is a reference I should be able to
take advantage of polymorphic behaviour, or not?

TIA
Henning


Maybe it has something to do with the relationship between virtual and
const. Does it crash on entry into the method or at a specific place
within the method?

Jun 24 '06 #2
doug turnbull wrote:

Maybe it has something to do with the relationship between virtual and
const. Does it crash on entry into the method or at a specific place
within the method?


I found the problem last night, it was the fault of that self-built
container (I made it too clever so it copied values if it got a const
one to insert, which made all const items to instances of their base
class only, addionally my copy-constructor was broken so I played around
with already deleted objects).

Thank you for your time anyway :-)

Henning
Jun 24 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Victor Irzak | last post by:
Hello, I have an ABC. it supports: ostream & operator << I also have a derived class that supports this operator. How can I call operator << of the base class for derived object??? Is it...
2
by: Trevor | last post by:
Hello, Please bear with me, I am trying to learn C++. I am implementing some error/debug functions which format a message and output it to a C++ stream. I would like to design it using one low...
1
by: Tim Partridge | last post by:
I want operator<< to be a friend function of a class inside a namespace, but I don't know how. For example: #include <iostream> namespace ns { class foo { public: // there is something...
1
by: learning_C++ | last post by:
Hi, I compiled some code. In the function friend ostream& operator<<(ostream& os, const complex c); I use the later argument complex c and complex& c. I can get the same values and there is no...
16
by: Steven T. Hatton | last post by:
In the following code, the only way I can figure out to pass an array of const is by setting the template argument to const in the instanciation expression. It would be (or seem to me) better if I...
4
by: Rock | last post by:
I'm in the process of writing this program for complex numbers and I use DevC++. My professor on the other hand compiles on Borland 5.5. So I ocasionally save and run my work on Borland to see if...
6
by: Geoffrey S. Knauth | last post by:
It's been a while since I programmed in C++, and the language sure has changed. Usually I can figure out why something no longer compiles, but this time I'm stumped. A friend has a problem he...
5
by: Matt | last post by:
I am trying to cast an ostream reference to void* and back again. The code below shows the problem isolated from a more complex program. It compiles quietly but seg faults upon execution. //...
2
by: waitan | last post by:
#include <algorithm> #include <iostream> #include <fstream> #include <string> #include <vector> #include <sstream> #include <iterator> #include <iomanip> using namespace std;
3
by: Martin T. | last post by:
Hello. I tried to overload the operator<< for implicit printing of wchar_t string on a char stream. Normally using it on a ostream will succeed as std::operator<<<std::char_traits<char> will...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.