473,387 Members | 1,664 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,387 software developers and data experts.

Converting a struct to a stream

I have created the following conversion in the header file
for my struct called SomeStruct.

operator std::string () {return "some useless text";}

I'm using it as follows.

SomeStruct someS;
std:cout << (std::string)someS;

This far, everything works just fine. Now, i got lazy of
constantly writing the string-part so i thought i'd like to
extend my struct to be convertible to a stream. So i
went as follows.

operator std::ostream () {return "some other text";}

This didn't work, however. I don't understand the error
messages either. Any hints?

Also, would it be a smarter idea to overload << operator
for my struct? How do i do that? I've played around
with friend-keyword but frankly, i must be missing
something, getting nowhere. Any help is appreciated...
--
Vänligen Kerstin Viltersten
(The Cool Giraffe)
Apr 2 '07 #1
7 3822
The Cool Giraffe wrote:
I have created the following conversion in the header file
for my struct called SomeStruct.

operator std::string () {return "some useless text";}

I'm using it as follows.

SomeStruct someS;
std:cout << (std::string)someS;

This far, everything works just fine. Now, i got lazy of
constantly writing the string-part so i thought i'd like to
extend my struct to be convertible to a stream. So i
went as follows.

operator std::ostream () {return "some other text";}

This didn't work, however. I don't understand the error
messages either. Any hints?

Also, would it be a smarter idea to overload << operator
for my struct?
Definitely.
How do i do that? I've played around
with friend-keyword but frankly, i must be missing
something, getting nowhere. Any help is appreciated...
You need a standalone function, not a member of a
class. Something like

ostream& operator<<(ostream& whither, const yourstruct& what) {
whither << "some useless text";
return whither;
}

HTH,
- J.
Apr 2 '07 #2
The Cool Giraffe wrote:
I have created the following conversion in the header file
for my struct called SomeStruct.

operator std::string () {return "some useless text";}

I'm using it as follows.

SomeStruct someS;
std:cout << (std::string)someS;

This far, everything works just fine. Now, i got lazy of
constantly writing the string-part so i thought i'd like to
extend my struct to be convertible to a stream. So i
went as follows.

operator std::ostream () {return "some other text";}

This didn't work, however. I don't understand the error
messages either. Any hints?

Also, would it be a smarter idea to overload << operator
for my struct? How do i do that? I've played around
with friend-keyword but frankly, i must be missing
something, getting nowhere. Any help is appreciated...

Try operator std::ostream & (std::ostream & os) {return os << "some
other text";}

First of all you can't construct std::ostream from char *, and secondly
std::ostream is not copy constructible. Your best bet is to override
'<<' such as:
operator std::ostream & << (std::ostream & os) { return os << "some text"; }

Fei
Apr 2 '07 #3
Jacek Dziedzic wrote:
You need a standalone function, not a member of a
class. Something like

ostream& operator<<(ostream& whither, const yourstruct& what) {

And probably (if you need access to private members):

class yourstruct
{
// your stuff
friend ostream& operator<<(ostream&, const yourstruct&);
}
Apr 2 '07 #4
Ralph D. Ungermann wrote/skrev/kaita/popisal/schreibt :
Jacek Dziedzic wrote:
> You need a standalone function, not a member of a
class. Something like

ostream& operator<<(ostream& whither, const yourstruct& what) {

And probably (if you need access to private members):

class yourstruct {
// your stuff
friend ostream& operator<<(ostream&, const yourstruct&); }

Thanks. Now, some wonderings i have about your answer.
1. What happened to the names of the parameters? I was
expecting something like the below.
class yourstruct {
// your stuff
friend ostream& operator<<(ostream& param1, const yourstruct& param2); }
2. How to implement the method itself? I'm guessing
somewhere in the lines of this:

param1 = "something";
return param1;

or perhaps

param1 << "something";
return param1;

but is the above a legal C++ code? I know some things
do work but are not really that good to use. Hence my
wondering. How would YOU implement that function?

--
Vänligen Kerstin Viltersten
(The Cool Giraffe)
Apr 2 '07 #5
The Cool Giraffe wrote:
Ralph D. Ungermann wrote/skrev/kaita/popisal/schreibt :
>Jacek Dziedzic wrote:
>> You need a standalone function, not a member of a
class. Something like

ostream& operator<<(ostream& whither, const yourstruct& what) {
And probably (if you need access to private members):

class yourstruct {
// your stuff
friend ostream& operator<<(ostream&, const yourstruct&); }


Thanks. Now, some wonderings i have about your answer.
1. What happened to the names of the parameters? I was
expecting something like the below.
In declarations you can omit them.
>class yourstruct {
// your stuff
friend ostream& operator<<(ostream& param1, const yourstruct& param2); }

2. How to implement the method itself? I'm guessing
somewhere in the lines of this:

param1 = "something";
return param1;
I don't think you can assign a string to a stream.
or perhaps

param1 << "something";
return param1;
This one.
but is the above a legal C++ code?
This one is. Remember to return the stream, this
will allow for chaining multiple <<'s together.
I know some things
do work but are not really that good to use. Hence my
wondering. How would YOU implement that function?
Just like you did in:
param1 << "something";
return param1;
BTW, you are not "converting" a struct to a stream,
rather sending it there or perhaps serializing would be
a better word.

HTH,
- J.
Apr 2 '07 #6
Jacek Dziedzic wrote/skrev/kaita/popisal/schreibt :
The Cool Giraffe wrote:
>Ralph D. Ungermann wrote/skrev/kaita/popisal/schreibt :
>>Jacek Dziedzic wrote:
>>> You need a standalone function, not a member of a
class. Something like

ostream& operator<<(ostream& whither, const yourstruct& what) {
And probably (if you need access to private members):

class yourstruct {
// your stuff
friend ostream& operator<<(ostream&, const yourstruct&); }


Thanks. Now, some wonderings i have about your answer.
1. What happened to the names of the parameters? I was
expecting something like the below.

In declarations you can omit them.
>>class yourstruct {
// your stuff
friend ostream& operator<<(ostream& param1, const yourstruct&
param2); }

2. How to implement the method itself? I'm guessing
somewhere in the lines of this:

param1 = "something";
return param1;

I don't think you can assign a string to a stream.
>or perhaps

param1 << "something";
return param1;

This one.
>but is the above a legal C++ code?

This one is. Remember to return the stream, this
will allow for chaining multiple <<'s together.
>I know some things
do work but are not really that good to use. Hence my
wondering. How would YOU implement that function?

Just like you did in:
>param1 << "something";
return param1;

BTW, you are not "converting" a struct to a stream,
rather sending it there or perhaps serializing would be
a better word.

Right, got it. Thank you very much.

--
Vänligen Kerstin Viltersten
(The Cool Giraffe)
Apr 2 '07 #7
The Cool Giraffe wrote:
Ralph D. Ungermann wrote/skrev/kaita/popisal/schreibt :
>>Jacek Dziedzic wrote:
>> You need [...] Something like

ostream& operator<<(ostream& whither, const yourstruct& what) {

And probably (if you need access to private members):

class yourstruct {
// your stuff
friend ostream& operator<<(ostream&, const yourstruct&); }

Thanks. Now, some wonderings i have about your answer.
1. What happened to the names of the parameters?
Feel free to provide argument names as you like. They have no effect,
unless you _define_ the function; in the body, these are the names you
have to use in your implementation (*).

I've omitted the names, because the types are self-explaining here.
But I'd definitely provide names there:
bool resize( int, int, int, int, bool );
2. How to implement the method itself?
ostream& operator<<(ostream& os, const yourstruct& what)
{
os << "{";
if ( what.ok() )
os << what.private_x << ", " << what.private_y;
else
os << "(undefined)";
os << "}"; // definitely NO std::endl here!

return os;
}

N.B.: Stream modifiers probably won't work as expected:
std::cout << std::setw(8) << yourstruct(17, 4) << std::endl;
// will look ugly

--
(*) If you omit an argument name in a function's _definition_, you can't
use it in the body. This is a common way to circumvent compiler warnings
about unused arguments.
Apr 2 '07 #8

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

Similar topics

56
by: ccwork | last post by:
Hi all, Here is a sample code segment: .... typedef PACKED struct { union { PACKED struct { char red:1;
5
by: nmtoan | last post by:
Hi, I could not find any answer to this simple question of mine. Suppose I have to write a program, the main parts of it are as follows: #include <blahblah.h> struct {
1
by: Rasika | last post by:
I have zip file called "PPC_DATA.zip" that contains four jpg images. Using the code below I can get a ZipEntry, which can be converted into a Stream by using zFile.GetInputStream(e). I want to...
3
by: Pete Davis | last post by:
I've never done this in C# so I don't know what the appropriate way of doing it is. I've got an array of bytes and I need to convert the array into "usable" data. For example, the first 4 bytes...
4
by: Abubakar | last post by:
Hi, I am writing a server in C# and client in C++ (pure, not managed). The communication goes on through sockets. In C# I am using NetworkStream to send text data (by converting it to byte array)...
6
by: rh1200la | last post by:
Hey All. I'm trying to download an image from a URL and display it in my page. I'm getting an error converting the Stream to Memory Stream. I'm getting a Specified cast is not valid error for...
3
by: Jef Driesen | last post by:
How can I convert a date string to a number (e.g. a time_t value or a tm struct)? I know about the strptime function, but then I have to know the format string. And that is a problem. I'm trying...
3
by: ashishgdata | last post by:
i have a text file which has a byte stream as follows - 0B00000000 0B01010101 0B00100100 and so on . the ' 0B ' are the starting bits and then each '0' or '1' stands for one pixel and a sing...
4
by: screamer81 | last post by:
Hello, I've a SDK functions description for a scanner and I try to convert unmanaged DLL C++ functions to c#. I converted all except one of them. This function is getting a struct parameter. ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
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?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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,...

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.