g++ compile error. help

P

pfsi

main.cpp
1: typedef struct A {
2: int x;
3: }A_t;
4:
5: int main() {
6: A_t *a;
7: a = &(A_t());
8: return 0;
9: }

---------------------

When I use g++(2.96) to compile this program, it report an error at line 7.
Error Message is : main.cpp:7: parse error before `;'
But this program can be pass in VC6, who can tell me why?
thanks.
 
K

Kevin Goodsell

pfsi said:
main.cpp
1: typedef struct A {
2: int x;
3: }A_t;

Please don't do this. struct tags are types in C++. This typedef
nonsense only obfuscates the code.

struct A_t
{
int x;
};
4:
5: int main() {
6: A_t *a;
7: a = &(A_t());

I would not expect this to compile. It looks like you are attempting to
take the address of a temporary. Any particular reason? I don't believe
it is legal to take a temporary's address, nor is it useful.
8: return 0;
9: }

---------------------

When I use g++(2.96) to compile this program, it report an error at line 7.
Error Message is : main.cpp:7: parse error before `;'
But this program can be pass in VC6, who can tell me why?
thanks.

My best guess is that VC6 is wrong.

-Kevin
 
A

Alan Morgan

Please don't do this. struct tags are types in C++. This typedef
nonsense only obfuscates the code.

struct A_t
{
int x;
};


I would not expect this to compile. It looks like you are attempting to
take the address of a temporary. Any particular reason? I don't believe
it is legal to take a temporary's address, nor is it useful.

Gcc issues a warning if you write the line like so:

7: a = &A_t();

Any idea why it gives a warning for one and a parse error for the other?

Alan
 
R

Ron Natalie

pfsi said:
7: a = &(A_t());
When I use g++(2.96) to compile this program, it report an error at line 7.
Error Message is : main.cpp:7: parse error before `;'
But this program can be pass in VC6, who can tell me why?
thanks.
VC6 is wrong. A_t() yields an rvalue. You can not apply & to it.
 

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,774
Messages
2,569,599
Members
45,162
Latest member
GertrudeMa
Top