 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Susanta Padhi Guest
|
Posted: Fri Aug 18, 2006 6:06 pm Post subject: Stream Manipulater in gcc 4.0 Linux Fedora core 4 |
|
|
{ to the OP: for Linux-specific questions (and answers) consider posting
to the Linux newsgroup, like 'comp.os.linux.development.apps'. -mod }
{ to All: let's stay on topic by giving platform-independent answers.
thanks! -mod }
Hi All,
I have thried bello code in AIX it works fine but in Linux gives
following errors.
******************Errror**************
tes.C:11: error: expected constructor, destructor, or type conversion
before ‘;’ token
tes.C:19: error: expected constructor, destructor, or type conversion
before ‘repeat’
tes.C: In function ‘int main()’:
tes.C:30: error: invalid initialization of non-const reference of type
‘std::ostream&’ from a temporary of type ‘const
char*’
*******************************************
**********************Code*****************************************
include<stdio.h>
#include <iostream.h>
#include <iomanip.h>
struct Repeatpair {
const char* s ;
int n ;
} ;
IOMANIPdeclare(Repeatpair) ;
ostream& repeat(ostream& o, Repeatpair p) {
// insert p.s into o, p.n times
for ( int n = p.n ; n > 0 ; --n ) o << p.s ;
return o ;
}
OMANIP(Repeatpair) repeat(const char* s, int n) {
Repeatpair p ;
p.s=s ; p.n=n ;
return OMANIP(Repeatpair)(repeat,p) ;
}
int main()
{
return 0;
struct Repeatpair a1 ={"sus", 5};
cout<<repeat("sus",5);
}
*************************************************
Expecting suggections to make it work on Linux.
Regards
Susnata
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ] |
|
| Back to top |
|
 |
Ulrich Eckhardt Guest
|
Posted: Fri Aug 18, 2006 10:28 pm Post subject: Re: Stream Manipulater in gcc 4.0 Linux Fedora core 4 |
|
|
Susanta Padhi wrote:
| Quote: | #include <iostream.h
#include <iomanip.h
|
Those two headers are pre-standard and not even shipped with recent
compilers anymore. I thought that GCC had dropped theset, too, so how did
you get this through the compiler? Could it be that you don't work with
warnings turned on ("-W -Wextra" for GCC)? You should, as it often gives
valuable hints.
| Quote: | struct Repeatpair {
const char* s ;
int n ;
} ;
IOMANIPdeclare(Repeatpair) ;
|
I guess this is some kind of macro, but what does it do? It's not part of
the C++ standard...
| Quote: | OMANIP(Repeatpair) repeat(const char* s, int n) {
Repeatpair p ;
p.s=s ; p.n=n ;
return OMANIP(Repeatpair)(repeat,p) ;
}
|
OMANIP() looks like another macro, what does that do? Incidentally, you
should be able to remove the IOMANIPdeclare() macro and write the repeat()
function like this:
Repeatpair repeat(char const* s, int n) {
Repeatpair p;
p.s = s;
p.n = n;
return p;
}
| Quote: | struct Repeatpair a1 ={"sus", 5};
|
FYI, in C++ as opposed to C you can drop the 'struct', 'class' (not present
in C), 'enum' or 'union' in front of such declarations. In C you can remove
the need for them via a typedef.
| Quote: | Expecting suggections to make it work on Linux.
|
Sorry but this has nothing to do with Linux, the code is a bit stale on any
halfway modern system. I'd suggest that you pick a good book about modern
C++ from the book reviews at accu.org.
Uli
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ] |
|
| Back to top |
|
 |
kanze Guest
|
Posted: Mon Aug 21, 2006 4:31 pm Post subject: Re: Stream Manipulater in gcc 4.0 Linux Fedora core 4 |
|
|
Ulrich Eckhardt wrote:
| Quote: | Susanta Padhi wrote:
#include <iostream.h
#include <iomanip.h
Those two headers are pre-standard and not even shipped with
recent compilers anymore. I thought that GCC had dropped
theset, too, so how did you get this through the compiler?
|
G++ has not supported them since version 3.0. It does have
incompatible headers with these names, however.
Given the code which follows, the original poster has two
solutions: install a pre 3.0 version of g++, or rewrite his
software to work with the standard headers. The second option
is obviously the only one which will work long term; other
compilers are dropping support for the pre-standard headers as
well. But in the short run, if schedules preclude rewriting all
of the code, the older version is the way to go.
Note that this is true for other compilers as well. Sun botched
the compatibility in the same way g++ did, so with Sun CC, it's
a choice of using compat=4 (effectively backdating the language
to pre-standard days) or a complete rewrite as well.
Of the compilers I regularly use, about the only one which
handled the transition in an anywhere near acceptable fashion
was VC++, where <iostream.h> gives you the old iostreams, and
<iostream> et al. the standard ones.
| Quote: | Could it be that you don't work with warnings turned on ("-W
-Wextra" for GCC)? You should, as it often gives valuable
hints.
struct Repeatpair {
const char* s ;
int n ;
} ;
IOMANIPdeclare(Repeatpair) ;
I guess this is some kind of macro, but what does it do? It's
not part of the C++ standard...
|
It was part of the de facto standard in pre-ISO days.
Note that this is related to a more general pre-ISO standard
header: <generic.h>, which simulates templates with macros. The
code in question here is probably very, very old. If I remember
right, even some pre-ISO compilers started using templates here.
| Quote: | OMANIP(Repeatpair) repeat(const char* s, int n) {
Repeatpair p ;
p.s=s ; p.n=n ;
return OMANIP(Repeatpair)(repeat,p) ;
}
OMANIP() looks like another macro, what does that do?
|
Again, a pre-ISO standard, pre-templates.
| Quote: | Incidentally, you should be able to remove the
IOMANIPdeclare() macro and write the repeat() function like
this:
Repeatpair repeat(char const* s, int n) {
Repeatpair p;
p.s = s;
p.n = n;
return p;
}
|
No. OMANIPdeclare and IOMANIP are part of a simulation of
templates, which create a class which uses Repeatpair, and
defined << and >> for it.
| Quote: | struct Repeatpair a1 ={"sus", 5};
FYI, in C++ as opposed to C you can drop the 'struct', 'class'
(not present in C), 'enum' or 'union' in front of such
declarations. In C you can remove the need for them via a
typedef.
Expecting suggections to make it work on Linux.
Sorry but this has nothing to do with Linux, the code is a bit
stale on any halfway modern system. I'd suggest that you pick
a good book about modern C++ from the book reviews at
accu.org.
|
The code is very old. The question is what to do about it.
Ideally, of course, a rewrite is in order, but schedule
constraits may make this problematic. If so, I don't see any
solution other than acquiring an older version of the compiler
somehow. Most vendors have been very irresponsible concerning a
migration path here. (To totally replace a working system with
a totally incompatible one from one version to the next, without
providing a migration path, is irresponsible.)
--
James Kanze GABI Software
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ] |
|
| Back to top |
|
 |
Carl Barron Guest
|
Posted: Tue Aug 22, 2006 12:41 am Post subject: Re: Stream Manipulater in gcc 4.0 Linux Fedora core 4 |
|
|
In article <1155890522.499462.286160 (AT) i42g2000cwa (DOT) googlegroups.com>,
Susanta Padhi <susantpadhi (AT) gmail (DOT) com> wrote:
| Quote: | { to the OP: for Linux-specific questions (and answers) consider
posting
to the Linux newsgroup, like
'comp.os.linux.development.apps'. -mod }
{ to All: let's stay on topic by giving platform-independent
answers.
thanks! -mod }
Hi All,
I have thried bello code in AIX it works fine but in Linux gives
following errors.
******************Errror**************
tes.C:11: error: expected constructor, destructor, or type
conversion
before ââ,¬Ëo;ââ,¬â"¢ token
tes.C:19: error: expected constructor, destructor, or type
conversion
before ââ,¬Ëorepeatââ,¬â"¢
tes.C: In function ââ,¬Ëoint main()ââ,¬â"¢:
tes.C:30: error: invalid initialization of non-const reference of
type
ââ,¬Ëostd::ostream&ââ,¬â"¢ from a temporary of type ââ,¬Ëoconst
char*ââ,¬â"¢
*******************************************
**********************Code*****************************************
include<stdio.h
#include <iostream.h
#include <iomanip.h
struct Repeatpair {
const char* s ;
int n ;
} ;
IOMANIPdeclare(Repeatpair) ;
ostream& repeat(ostream& o, Repeatpair p) {
// insert p.s into o, p.n times
for ( int n = p.n ; n > 0 ; --n ) o << p.s ;
return o ;
}
OMANIP(Repeatpair) repeat(const char* s, int n) {
Repeatpair p ;
p.s=s ; p.n=n ;
return OMANIP(Repeatpair)(repeat,p) ;
}
int main()
{
return 0;
struct Repeatpair a1 ={"sus", 5};
cout<<repeat("sus",5);
}
*************************************************
Expecting suggections to make it work on Linux.
how about creating a temp of a specific struct with an operator |
overload. Then the manipulation becomes merely outputing a temp
created
by repeater's ctor.
This should work either in the old or new system.
template <class T>
struct repeater
{
T x;
int n;
repeater(const T &a,int b) (a),n(b){}
}
template <class T>
std::ostream & operator << (std::ostream &os,const repeater<T> &r)
{
for(int i=0;i!=r.n) {os << r.x;}
return os;
}
template <class T>
inline repeater<T> repeat(const T &x,int n)
{
return repeater<T>(x,n);
}
depending on the age of the compilers you might need to manually
expand these templates, or provide char * overloads.
Usage example:
os << repeat("abc",5) << '\n';
what am I missing???
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ] |
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|