By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,413 Members | 996 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,413 IT Pros & Developers. It's quick & easy.

weird warning with g++ 4.1

P: n/a
the following program does not compile with g++ 4.1.1:
================================================== ==
// $Id$
// $Source$

#include <stdint.h>
#include <stdio.h>

typedef struct { uint32_t one_c; } chart;
#define as_chart(c) ((chart){one_c:(c)})
#define ascii(x) as_chart((uint8_t)(x))
#define CR 13
#define LF 10

static void wr_ch_unbuffered_dos (void) {
static chart const crlf[2] = { ascii(CR), ascii(LF) };
for (int i = 0; i < 2; i++)
printf("%d\n",crlf[i].one_c);
}

int main (int argc, char *argv[]) {
printf("%d %s\n",argc,argv[0]);
wr_ch_unbuffered_dos();
return 0;
}
================================================== ==
cpp-struct.cc: In function 'void wr_ch_unbuffered_dos()':
cpp-struct.cc:14: warning: missing braces around initializer for 'const chart'
cpp-struct.cc:14: warning: missing braces around initializer for 'const chart'
cpp-struct.cc:14: error: cannot convert 'chart' to 'uint32_t' in initialization
cpp-struct.cc:14: error: cannot convert 'chart' to 'uint32_t' in initialization

the bad line is
static chart const crlf[2] = { ascii(CR), ascii(LF) };

I am pretty sure code like this worked with g++ 3.
What do I do now?
Thanks!

$ g++ --version
g++ (GCC) 4.1.1 20060525 (Red Hat 4.1.1-1)

--
Sam Steingold (http://www.podval.org/~sds) on Fedora Core release 5 (Bordeaux)
http://palestinefacts.org http://camera.org http://truepeace.org
http://israelunderattack.slide.com http://dhimmi.com http://pmw.org.il
The paperless office will become a reality soon after the paperless toilet.
Sep 27 '06 #1
Share this Question
Share on Google+
13 Replies


P: n/a
Sam Steingold wrote:
the following program does not compile with g++ 4.1.1:
================================================== ==
// $Id$
// $Source$

#include <stdint.h>
#include <stdio.h>

typedef struct { uint32_t one_c; } chart;
#define as_chart(c) ((chart){one_c:(c)})
#define ascii(x) as_chart((uint8_t)(x))
#define CR 13
#define LF 10

static void wr_ch_unbuffered_dos (void) {
static chart const crlf[2] = { ascii(CR), ascii(LF) };
for (int i = 0; i < 2; i++)
printf("%d\n",crlf[i].one_c);
}

int main (int argc, char *argv[]) {
printf("%d %s\n",argc,argv[0]);
wr_ch_unbuffered_dos();
return 0;
}
================================================== ==
cpp-struct.cc: In function 'void wr_ch_unbuffered_dos()':
cpp-struct.cc:14: warning: missing braces around initializer for 'const chart'
cpp-struct.cc:14: warning: missing braces around initializer for 'const chart'
cpp-struct.cc:14: error: cannot convert 'chart' to 'uint32_t' in initialization
cpp-struct.cc:14: error: cannot convert 'chart' to 'uint32_t' in initialization

the bad line is
static chart const crlf[2] = { ascii(CR), ascii(LF) };

I am pretty sure code like this worked with g++ 3.
What do I do now?
Thanks!

$ g++ --version
g++ (GCC) 4.1.1 20060525 (Red Hat 4.1.1-1)
Looks like you're trying to use C99 features that C++ does not support.
Look for a compiler flag to enable those extensions, or change your
code to valid C++ syntax.

Cheers! --M

Sep 27 '06 #2

P: n/a
* mlimber <zy*****@tznvy.pbz[2006-09-27 07:40:58 -0700]:
>
Sam Steingold wrote:
>the following program does not compile with g++ 4.1.1:
================================================= ===
// $Id$
// $Source$

#include <stdint.h>
#include <stdio.h>

typedef struct { uint32_t one_c; } chart;
#define as_chart(c) ((chart){one_c:(c)})
#define ascii(x) as_chart((uint8_t)(x))
#define CR 13
#define LF 10

static void wr_ch_unbuffered_dos (void) {
static chart const crlf[2] = { ascii(CR), ascii(LF) };
for (int i = 0; i < 2; i++)
printf("%d\n",crlf[i].one_c);
}

int main (int argc, char *argv[]) {
printf("%d %s\n",argc,argv[0]);
wr_ch_unbuffered_dos();
return 0;
}
================================================= ===
cpp-struct.cc: In function 'void wr_ch_unbuffered_dos()':
cpp-struct.cc:14: warning: missing braces around initializer for 'const chart'
cpp-struct.cc:14: warning: missing braces around initializer for 'const chart'
cpp-struct.cc:14: error: cannot convert 'chart' to 'uint32_t' in initialization
cpp-struct.cc:14: error: cannot convert 'chart' to 'uint32_t' in initialization

the bad line is
static chart const crlf[2] = { ascii(CR), ascii(LF) };

I am pretty sure code like this worked with g++ 3.
What do I do now?
Thanks!

$ g++ --version
g++ (GCC) 4.1.1 20060525 (Red Hat 4.1.1-1)

Looks like you're trying to use C99 features that C++ does not support.
Look for a compiler flag to enable those extensions, or change your
code to valid C++ syntax.
what would be "valid C++ syntax" in this case?

--
Sam Steingold (http://www.podval.org/~sds) on Fedora Core release 5 (Bordeaux)
http://honestreporting.com http://camera.org http://thereligionofpeace.com
http://pmw.org.il http://memri.org http://israelunderattack.slide.com
A poet who reads his verse in public may have other nasty habits.
Sep 27 '06 #3

P: n/a
Sam Steingold wrote:
>* mlimber <zy*****@tznvy.pbz[2006-09-27 07:40:58 -0700]:

Sam Steingold wrote:
>>the following program does not compile with g++ 4.1.1:
================================================ ====
// $Id$
// $Source$

#include <stdint.h>
#include <stdio.h>

typedef struct { uint32_t one_c; } chart;
#define as_chart(c) ((chart){one_c:(c)})
#define ascii(x) as_chart((uint8_t)(x))
#define CR 13
#define LF 10

static void wr_ch_unbuffered_dos (void) {
static chart const crlf[2] = { ascii(CR), ascii(LF) };
for (int i = 0; i < 2; i++)
printf("%d\n",crlf[i].one_c);
}

int main (int argc, char *argv[]) {
printf("%d %s\n",argc,argv[0]);
wr_ch_unbuffered_dos();
return 0;
}
================================================ ====
cpp-struct.cc: In function 'void wr_ch_unbuffered_dos()':
cpp-struct.cc:14: warning: missing braces around initializer for
'const chart' cpp-struct.cc:14: warning: missing braces around
initializer for 'const chart' cpp-struct.cc:14: error: cannot
convert 'chart' to 'uint32_t' in initialization cpp-struct.cc:14:
error: cannot convert 'chart' to 'uint32_t' in initialization

the bad line is
static chart const crlf[2] = { ascii(CR), ascii(LF) };

I am pretty sure code like this worked with g++ 3.
What do I do now?
Thanks!

$ g++ --version
g++ (GCC) 4.1.1 20060525 (Red Hat 4.1.1-1)

Looks like you're trying to use C99 features that C++ does not
support. Look for a compiler flag to enable those extensions, or
change your code to valid C++ syntax.

what would be "valid C++ syntax" in this case?
Not sure what you're trying to accomplish with it, but on
a 32-bit machine, it's something like

// there is no <stdint.hin C++, but here is the approximation
typedef unsigned uint32_t;
typedef unsigned char uint8_t;

#include <stdio.h>

struct chart { uint32_t one_c; };

#define as_chart(c) (c)
#define ascii(x) as_chart(uint8_t(x))
#define CR 13
#define LF 10

static void wr_ch_unbuffered_dos (void) {
static chart const crlf[2] = { ascii(CR), ascii(LF) };
for (int i = 0; i < 2; i++)
printf("%d\n",crlf[i].one_c);
}

int main (int argc, char *argv[]) {
printf("%d %s\n",argc,argv[0]);
wr_ch_unbuffered_dos();
return 0;
}

Note, however, that 'chart' should probably have a constructor,
but since it changes its nature, adding a c-tor wasn't done.
You need to state your intentions and then we can help you find
a proper C++ solution.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Sep 27 '06 #4

P: n/a
Sam Steingold wrote:
* mlimber <zy*****@tznvy.pbz[2006-09-27 07:40:58 -0700]:

Sam Steingold wrote:
the following program does not compile with g++ 4.1.1:
================================================== ==
// $Id$
// $Source$

#include <stdint.h>
#include <stdio.h>

typedef struct { uint32_t one_c; } chart;
#define as_chart(c) ((chart){one_c:(c)})
#define ascii(x) as_chart((uint8_t)(x))
#define CR 13
#define LF 10

static void wr_ch_unbuffered_dos (void) {
static chart const crlf[2] = { ascii(CR), ascii(LF) };
for (int i = 0; i < 2; i++)
printf("%d\n",crlf[i].one_c);
}

int main (int argc, char *argv[]) {
printf("%d %s\n",argc,argv[0]);
wr_ch_unbuffered_dos();
return 0;
}
================================================== ==
cpp-struct.cc: In function 'void wr_ch_unbuffered_dos()':
cpp-struct.cc:14: warning: missing braces around initializer for 'const chart'
cpp-struct.cc:14: warning: missing braces around initializer for 'const chart'
cpp-struct.cc:14: error: cannot convert 'chart' to 'uint32_t' in initialization
cpp-struct.cc:14: error: cannot convert 'chart' to 'uint32_t' in initialization

the bad line is
static chart const crlf[2] = { ascii(CR), ascii(LF) };

I am pretty sure code like this worked with g++ 3.
What do I do now?
Thanks!

$ g++ --version
g++ (GCC) 4.1.1 20060525 (Red Hat 4.1.1-1)
Looks like you're trying to use C99 features that C++ does not support.
Look for a compiler flag to enable those extensions, or change your
code to valid C++ syntax.

what would be "valid C++ syntax" in this case?
Well, technically stdint.h isn't part of C++, but assuming that
extension exists, you could make your code:

#include <stdint.h>
#include <stdio.h>

typedef struct { uint32_t one_c; } chart;
//#define as_chart(c) ((chart){one_c:(c)}) // Unneeded
//#define ascii(x) as_chart((uint8_t)(x)) // Unneeded
#define CR 13
#define LF 10

static void wr_ch_unbuffered_dos (void) {
static chart const crlf[2] = { {CR}, {LF} }; // Change this
for (int i = 0; i < 2; i++)
printf("%u\n",crlf[i].one_c); // Note format string
}

Of course, if you were going all C++, I'd make some other changes
(viz., convert the typedef to an ordinary struct definition, get rid of
the macros, drop the "static" which has been deprecated in favor of
anonymous namespaces, use iostreams instead of printf [cf.
http://www.parashift.com/c++-faq-lit...html#faq-15.1], and
drop the "void" in parentheses).

Cheers! --M

Sep 27 '06 #5

P: n/a
mlimber wrote:
Of course, if you were going all C++, I'd make some other changes
(viz., convert the typedef to an ordinary struct definition, get rid of
the macros, drop the "static" which has been deprecated in favor of
anonymous namespaces, use iostreams instead of printf [cf.
http://www.parashift.com/c++-faq-lit...html#faq-15.1], and
drop the "void" in parentheses).
Oh, and add a constructor like Victor said.

Cheers! --M

Sep 27 '06 #6

P: n/a
* Victor Bazarov <i.********@pbzNpnfg.arg[2006-09-27 11:44:39 -0400]:
>
Sam Steingold wrote:
>>* mlimber <zy*****@tznvy.pbz[2006-09-27 07:40:58 -0700]:

Sam Steingold wrote:
the following program does not compile with g++ 4.1.1:
=============================================== =====
// $Id$
// $Source$

#include <stdint.h>
#include <stdio.h>

typedef struct { uint32_t one_c; } chart;
#define as_chart(c) ((chart){one_c:(c)})
#define ascii(x) as_chart((uint8_t)(x))
#define CR 13
#define LF 10

static void wr_ch_unbuffered_dos (void) {
static chart const crlf[2] = { ascii(CR), ascii(LF) };
for (int i = 0; i < 2; i++)
printf("%d\n",crlf[i].one_c);
}

int main (int argc, char *argv[]) {
printf("%d %s\n",argc,argv[0]);
wr_ch_unbuffered_dos();
return 0;
}
=============================================== =====
cpp-struct.cc: In function 'void wr_ch_unbuffered_dos()':
cpp-struct.cc:14: warning: missing braces around initializer for
'const chart' cpp-struct.cc:14: warning: missing braces around
initializer for 'const chart' cpp-struct.cc:14: error: cannot
convert 'chart' to 'uint32_t' in initialization cpp-struct.cc:14:
error: cannot convert 'chart' to 'uint32_t' in initialization

the bad line is
static chart const crlf[2] = { ascii(CR), ascii(LF) };

I am pretty sure code like this worked with g++ 3.
What do I do now?
Thanks!

$ g++ --version
g++ (GCC) 4.1.1 20060525 (Red Hat 4.1.1-1)
Not sure what you're trying to accomplish with it,
CLISP (http://clisp.cons.org) is normally compiled with C, but it should
be compilable with C++ to enable some compile-time as well as run-time
checks for debugging. it has always compiled with g++, but I cannot
compile it with g++ 4.1.1 - specifically, I get the errors mentioned above.

note that if I replace

#define as_chart(c) ((chart){one_c:(c)})

with

extern __inline__ chart as_chart (register cint c)
{ register chart ch; ch.one_c = c; return ch; }

it becomes compilable with g++ 4.1

note also that it is only nested initializations that do not work.

chart foo = ascii(60);

works just fine.
so, I should have formulated my question like this: how do I modify
as_chart so that g++ 4.1.1 will accept it?

I am not interested in C++ bells and whistles here (they ARE used
elsewhere, but I do not need them at this place), all I care is how to
make the code compile.

thanks for your kind help/

--
Sam Steingold (http://www.podval.org/~sds) on Fedora Core release 5 (Bordeaux)
http://dhimmi.com http://memri.org http://openvotingconsortium.org
http://iris.org.il http://jihadwatch.org http://israelnorthblog.livejournal.com
Money does not "play a role", it writes the scenario.
Sep 27 '06 #7

P: n/a
Sam Steingold wrote:
CLISP (http://clisp.cons.org) is normally compiled with C, but it should
be compilable with C++ to enable some compile-time as well as run-time
checks for debugging. it has always compiled with g++, but I cannot
compile it with g++ 4.1.1 - specifically, I get the errors mentioned above.

note that if I replace

#define as_chart(c) ((chart){one_c:(c)})
This is not valid C++ syntax. If g++ 3.x supported it, it was by
extension.
>
with

extern __inline__ chart as_chart (register cint c)
{ register chart ch; ch.one_c = c; return ch; }
Extern and inline? BTW, __inline__ is non-standard, and using the
register keyword may in fact just confuse the optimizer. Best to leave
that off.
it becomes compilable with g++ 4.1

note also that it is only nested initializations that do not work.

chart foo = ascii(60);

works just fine.
so, I should have formulated my question like this: how do I modify
as_chart so that g++ 4.1.1 will accept it?
Get rid of the one_c:{c} business. That's the problem.
I am not interested in C++ bells and whistles here (they ARE used
elsewhere, but I do not need them at this place), all I care is how to
make the code compile.
See my other post.

Cheers! --M

Sep 27 '06 #8

P: n/a
* mlimber <zy*****@tznvy.pbz[2006-09-27 09:16:44 -0700]:
>
>so, I should have formulated my question like this: how do I modify
as_chart so that g++ 4.1.1 will accept it?

Get rid of the one_c:{c} business. That's the problem.
I don't have "one_c:{c}".
I have "((chart){one_c:(c)})".
what do I replace it with?
a constructor?
how do I write it?
thanks.
>I am not interested in C++ bells and whistles here (they ARE used
elsewhere, but I do not need them at this place), all I care is how to
make the code compile.

See my other post.
which one?

--
Sam Steingold (http://www.podval.org/~sds) on Fedora Core release 5 (Bordeaux)
http://palestinefacts.org http://ffii.org http://israelunderattack.slide.com
http://pmw.org.il http://thereligionofpeace.com http://truepeace.org
If abortion is murder, then oral sex is cannibalism.
Sep 27 '06 #9

P: n/a
Sam Steingold wrote:
* mlimber <zy*****@tznvy.pbz[2006-09-27 09:16:44 -0700]:
See my other post.

which one?
This one:

http://groups.google.com/group/comp....dd44249bffc950

Cheers! --M

Sep 27 '06 #10

P: n/a
Sam Steingold wrote:
>* mlimber <zy*****@tznvy.pbz[2006-09-27 09:16:44 -0700]:
>>so, I should have formulated my question like this: how do I modify
as_chart so that g++ 4.1.1 will accept it?

Get rid of the one_c:{c} business. That's the problem.

I don't have "one_c:{c}".
I have "((chart){one_c:(c)})".

Seems to me like the same, just with a cast in front of it.
what do I replace it with?
That depends on what it's supposed to do.

Sep 27 '06 #11

P: n/a
* Rolf Magnus <en******@g-bayvar.qr[2006-09-27 19:24:54 +0200]:
>
Sam Steingold wrote:
>>* mlimber <zy*****@tznvy.pbz[2006-09-27 09:16:44 -0700]:

so, I should have formulated my question like this: how do I modify
as_chart so that g++ 4.1.1 will accept it?

Get rid of the one_c:{c} business. That's the problem.

what do I replace it with?

That depends on what it's supposed to do.
convert an integer to a structure:

typedef struct { int one_c; } chart;
#define as_chart(c) ((chart){one_c:(c)})

as_chart should convert an integer to a struct.
--
Sam Steingold (http://www.podval.org/~sds) on Fedora Core release 5 (Bordeaux)
http://iris.org.il http://mideasttruth.com http://israelunderattack.slide.com
http://honestreporting.com http://dhimmi.com http://jihadwatch.org
The world is coming to an end. Please log off.
Sep 27 '06 #12

P: n/a
Sam Steingold wrote:
>* Rolf Magnus <en******@g-bayvar.qr[2006-09-27 19:24:54 +0200]:

Sam Steingold wrote:
>>>* mlimber <zy*****@tznvy.pbz[2006-09-27 09:16:44 -0700]:

so, I should have formulated my question like this: how do I modify
as_chart so that g++ 4.1.1 will accept it?
Get rid of the one_c:{c} business. That's the problem.
what do I replace it with?
That depends on what it's supposed to do.

convert an integer to a structure:

typedef struct { int one_c; } chart;
#define as_chart(c) ((chart){one_c:(c)})

as_chart should convert an integer to a struct.
How about:

struct chart { chart() {} chart(int c) : one_c(c) {} int one_c; };
inline chart as_chart(int c) { return chart(c); }
Sep 27 '06 #13

P: n/a
* Nate Barney <an********@tznvy.pbz[2006-09-27 13:58:26 -0400]:
>
Sam Steingold wrote:
>>* Rolf Magnus <en******@g-bayvar.qr[2006-09-27 19:24:54 +0200]:

Sam Steingold wrote:

* mlimber <zy*****@tznvy.pbz[2006-09-27 09:16:44 -0700]:
>
>so, I should have formulated my question like this: how do I modify
>as_chart so that g++ 4.1.1 will accept it?
Get rid of the one_c:{c} business. That's the problem.
what do I replace it with?
That depends on what it's supposed to do.

convert an integer to a structure:

typedef struct { int one_c; } chart;
#define as_chart(c) ((chart){one_c:(c)})

as_chart should convert an integer to a struct.

How about:

struct chart { chart() {} chart(int c) : one_c(c) {} int one_c; };
inline chart as_chart(int c) { return chart(c); }
this works, thanks!

--
Sam Steingold (http://www.podval.org/~sds) on Fedora Core release 5 (Bordeaux)
http://mideasttruth.com http://palestinefacts.org http://jihadwatch.org
http://truepeace.org http://israelnorthblog.livejournal.com http://iris.org.il
You think Oedipus had a problem -- Adam was Eve's mother.
Sep 27 '06 #14

This discussion thread is closed

Replies have been disabled for this discussion.