Connecting Tech Pros Worldwide Forums | Help | Site Map

help needed: defining my own ostream manipulators

paul.wilkins@gmail.com
Guest
 
Posts: n/a
#1: Jul 23 '05
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;
}


Mike Wahler
Guest
 
Posts: n/a
#2: Jul 23 '05

re: help needed: defining my own ostream manipulators


<paul.wilkins@gmail.com> wrote in message
news:1105991110.684526.216760@f14g2000cwb.googlegr oups.com...[color=blue]
> 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++.[/color]

"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.

[color=blue]
> Please help me port this code.
>
> Thanks,
> -Paul
>
>
> #include <iostream.h>
> #include <iomanip.h>[/color]

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'.
[color=blue]
>
> int x = 1;
> ios&[/color]

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>.
[color=blue]
> 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); }[/color]

There is no standard type 'smanip', nor have you provide
a definition of one.
[color=blue]
>
> 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;
> }[/color]

Book suggestions:
www.josuttis.com/libbook
http://www.langer.camelot.de/iostreams.html

-Mike



Jonathan Turkanis
Guest
 
Posts: n/a
#3: Jul 23 '05

re: help needed: defining my own ostream manipulators


Mike Wahler wrote:
[color=blue]
> 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.[/color]

The C90 headers stdio.h, stddef.h, stdlib.h, etc. are part of the C++ standard
library, although they are deprecated.
[color=blue]
> All of the library names
> (except macros) are defined in namespace 'std'.[/color]

Names from the above headers are not in std.

And don't forget rel_ops!

Jonathan


Dietmar Kuehl
Guest
 
Posts: n/a
#4: Jul 23 '05

re: help needed: defining my own ostream manipulators


paul.wilkins@gmail.com wrote:[color=blue]
> 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); }[/color]

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

Closed Thread