help needed: defining my own ostream manipulators

P

paul.wilkins

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;
}
 
M

Mike Wahler

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') said:
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
 
J

Jonathan Turkanis

Mike said:
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
 
D

Dietmar Kuehl

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::eek:stream& operator<< (std::eek:stream& out, my_setw const& m) {
| if (x)
| out.width(m.m_val);
| return out;
| }
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top