f



gcc version 3.2.3 20030502 (Red Hat Linux 3.2.3-34)

Hi.

Could someone please lend me a hand with a simple g++ problem.
Be so kind and consider the following code:

class A
{
public:
  A() {}
};

int main()
{
  (A());
  return 0;
}

Yields the following error using g++:

In function `int main()':
syntax error before `;' token

It seems to me, the compiler considers (A()) to be a cast rather than a
constructor? Does this g++ "misbehaviour" conform to the C++ standard
or is this a bug in g++?

thanks in advance,
Stoyan.

0
8/26/2005 9:48:30 AM
comp.lang.c++ 49423 articles. 7 followers. Post Follow

9 Replies
799 Views

Similar Articles

[PageSpeed] 32

On 26 Aug 2005 02:48:30 -0700
"nymano" <plokowitsch@hotmail.com> wrote:

> class A
> {
> public:
>   A() {}
> };
>=20
> int main()
> {
>   (A());

You are calling function void A(void) here!

For a constructor to be called you need to properly create an object of
class A:

A objectOfClassA;

The call for objectOfClassA's default constructor is implicit here,
by which I mean that the compiler automatically calls it when creating
an object of class A!

best regards / Gru=DF
Moritz Beller
--
web	http://www.4momo.de
mail	momo dot beller at t-online dot de
gpgkey	http://gpg.notlong.com
0
8/26/2005 10:10:47 AM
The code I posted is an example constructed only to illustrate the
problem. I'm NOT trying to declare a local variable.

Let me rephrase. The following statements create temporary ints. The
first statement uses the default constructor:

int();
int(1);

However, putting brackets around these expressions causes the error:

(int());
(int(1));

Only the expression with the default constructor causes the error. The
second statement compiles fine. Why is that so with g++? Other
compilers (mvsc, ibm/zos) have no problem.

stoyan.

0
8/26/2005 10:15:28 AM
No. Actually I am not calling a function void A(void) here put creating
a temporary object A. Please consult a C++ book on temporary objects.

0
8/26/2005 10:19:15 AM
"nymano" <plokowitsch@hotmail.com> wrote in message 
news:1125049710.229782.149390@g43g2000cwa.googlegroups.com...
> Hi.
>
> Could someone please lend me a hand with a simple g++ problem.
> Be so kind and consider the following code:
>
> class A
> {
> public:
>  A() {}
> };
>
> int main()
> {
>  (A());
>  return 0;
> }
>
> Yields the following error using g++:

Which version? Using g++ 3.4.2, it compiles successfully.

> In function `int main()':
> syntax error before `;' token
>
> It seems to me, the compiler considers (A()) to be a cast rather than a
> constructor? Does this g++ "misbehaviour" conform to the C++ standard
> or is this a bug in g++?

I suspect it is a bug in the version of g++ that you are currently using. 
Also, the code has managed to compile with the other compilers I have put it 
through -- MSVC++ 7.1 and Comeau C++ 4.3.3.

Regards,
Sumit.
-- 
Sumit Rajan <sumit.rajan@gmail.com> 


0
sumit.rajan (213)
8/26/2005 10:38:11 AM
nymano wrote:
<snip>
> Only the expression with the default constructor causes
> the error. The second statement compiles fine. Why is
> that so with g++? Other compilers (mvsc, ibm/zos) have
> no problem.
<snip>

Data point:

--foo.cpp--
int main() {
    ( int() );
    ( int( 1 ) );
}
--end foo.cpp--

for v in 2.95 3.{0,2,3,4} 4.0; do
  echo $v:
  g++-$v -o foo.{o,cpp} 
done
2.95:
foo.cpp: In function `int main()':
foo.cpp:2: parse error before `;'
3.0:
foo.cpp: In function `int main()':
foo.cpp:2: parse error before `;' token
3.2:
foo.cpp: In function `int main()':
foo.cpp:2: syntax error before `;' token
3.3:
foo.cpp: In function `int main()':
foo.cpp:2: error: syntax error before `;' token
3.4:
4.0:

Conclusion: Upgrade gcc to >= 3.4 if you need this.

Marc

0
marc2631 (44)
8/26/2005 11:44:31 AM
The compiler I've been using is:

gcc version 3.2.3 20030502 (Red Hat Linux 3.2.3-34)

I'll upgrade to 3.4.2 and try again.

In the meanwhile, thanks.
stoyan.

0
8/26/2005 11:50:07 AM
"nymano" <plokowitsch@hotmail.com> wrote in message 
news:1125051328.920974.300580@g43g2000cwa.googlegroups.com...
> The code I posted is an example constructed only to illustrate the
> problem. I'm NOT trying to declare a local variable.
>
> Let me rephrase. The following statements create temporary ints. The
> first statement uses the default constructor:
>
> int();
> int(1);
>
> However, putting brackets around these expressions causes the error:
>
> (int());
> (int(1));
>
> Only the expression with the default constructor causes the error. The
> second statement compiles fine. Why is that so with g++? Other
> compilers (mvsc, ibm/zos) have no problem.
>
> stoyan.
>

This seems to be a bug in this specific version of GCC, because (int()); and 
int(); are equivalent and should be treated in the same way after parsing. 
To me it seems that the compiler somehow prematurely tries to assemble a 
cast instead of dropping the superfluous parentheses. Did you check the GCC 
bug-archive, as this version is already dated?

Cheers
Chris 


0
8/26/2005 12:01:20 PM
> It seems to me, the compiler considers (A()) to be a cast rather than a
> constructor? Does this g++ "misbehaviour" conform to the C++ standard
> or is this a bug in g++?

just what I assumed in my first post;)
i'll upgrade my g++.

thanks
stoyan.

0
8/26/2005 12:25:12 PM
nymano wrote:

> Hi.
> 
> Could someone please lend me a hand with a simple g++ problem.
> Be so kind and consider the following code:
> 
> class A
> {
> public:
>   A() {}
> };
> 
> int main()
> {
>   (A());
what are you trying to achieve?
try 
A a;
>   return 0;
> }
> 
> Yields the following error using g++:
> 
> In function `int main()':
> syntax error before `;' token
> 
> It seems to me, the compiler considers (A()) to be a cast rather than a
> constructor? Does this g++ "misbehaviour" conform to the C++ standard
> or is this a bug in g++?
> 
> thanks in advance,
> Stoyan.

-- 
http://www.gregerhaga.net
http://skpp.sourceforge.net
http://xpman.sourceforge.net
0
bobsmith1 (54)
8/26/2005 12:27:18 PM
Reply: