help needed: defining my own ostream manipulators



 DEVELOP > c-Plus-Plus > help needed: defining my own ostream manipulators

LINK TO THIS PAGE  


rating :  0   |  0


  Page 1 of 1

1

 
Topic: DEVELOP > c-Plus-Plus
User: ""
Date: 17 Jan 2005 01:45:10 PM
Object: help needed: defining my own ostream manipulators
Hi,
I have a function I have written that works with g++ 2.95.3 that I
would like to use with newer g++ compilers >= 3.2.0. The function is a
stream manipulator similar to setw(). The code does not compile with
newer versions of g++. Please help me port this code.
Thanks,
-Paul
#include <iostream.h>
#include <iomanip.h>
int x = 1;
ios& temp_setw(ios & o, int n) { if(x) o.width(n); return o; }
smanip<int> my_setw(int n) { return smanip<int> (temp_setw,n); }
int main(int argc,char *argv[]) {
int a = 234;
x = 1; cout << a << my_setw(11) << "test" << endl;
x = 0; cout << a << my_setw(11) << "test" << endl;
}
.

User: "Mike Wahler"

Title: Re: help needed: defining my own ostream manipulators 17 Jan 2005 02:11:29 PM
<paul.wilkins@gmail.com> wrote in message
news:1105991110.684526.216760@f14g2000cwb.googlegroups.com...

Hi,

I have a function I have written that works with g++ 2.95.3 that I
would like to use with newer g++ compilers >= 3.2.0. The function is a
stream manipulator similar to setw(). The code does not compile with
newer versions of g++.

"does not compile" gives no useful information for us
to help you. However, what you've posted below is
not standard C++, which is what we discuss here.

Please help me port this code.

Thanks,
-Paul


#include <iostream.h>
#include <iomanip.h>

Neither of these headers are, or ever have been part of
standard C++. The corresponding standard headers are
<iostream> and <iomanip> (none of the standard headers
have ".h" in their names. All of the library names
(except macros) are defined in namespace 'std'.


int x = 1;
ios&

The standard type 'ios' has the full name 'std::ios',
(which is actually a specialization of the template
'std::basic_ios'), and is declared by header <ios>.

temp_setw(ios & o, int n) { if(x) o.width(n); return o; }
smanip<int> my_setw(int n) { return smanip<int> (temp_setw,n); }

There is no standard type 'smanip', nor have you provide
a definition of one.


int main(int argc,char *argv[]) {
int a = 234;
x = 1; cout << a << my_setw(11) << "test" << endl;
x = 0; cout << a << my_setw(11) << "test" << endl;
}

Book suggestions:
www.josuttis.com/libbook
http://www.langer.camelot.de/iostreams.html
-Mike
.
User: "Jonathan Turkanis"

Title: Re: help needed: defining my own ostream manipulators 17 Jan 2005 09:42:52 PM
Mike Wahler wrote:

Neither of these headers are, or ever have been part of
standard C++. The corresponding standard headers are
<iostream> and <iomanip> (none of the standard headers
have ".h" in their names.

The C90 headers stdio.h, stddef.h, stdlib.h, etc. are part of the C++ standard
library, although they are deprecated.

All of the library names
(except macros) are defined in namespace 'std'.

Names from the above headers are not in std.
And don't forget rel_ops!
Jonathan
.


User: "Dietmar Kuehl"

Title: Re: help needed: defining my own ostream manipulators 18 Jan 2005 05:07:43 AM
wrote:

int x = 1;
ios& temp_setw(ios & o, int n) { if(x) o.width(n); return o; }
smanip<int> my_setw(int n) { return smanip<int> (temp_setw,n); }

The predefined manipulators are not part of standard C++. You need
to provide them yourself. The simplest approach is something like
this:
| struct my_setw {
| my_setw(int i): m_val(i) {}
| };
| std::ostream& operator<< (std::ostream& out, my_setw const& m) {
| if (x)
| out.width(m.m_val);
| return out;
| }
--
<mailto:dietmar_kuehl@yahoo.com> <http://www.dietmar-kuehl.de/>
<http://www.contendix.com> - Software Development & Consulting
.


  Page 1 of 1

1

 


Related Articles
 

NEWER

pg.1232     pg.940     pg.716     pg.544     pg.412     pg.311     pg.234     pg.175     pg.130     pg.96     pg.70     pg.50     pg.35     pg.24     pg.16     pg.10     pg.6     pg.3     pg.1

OLDER