Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > gcc version 3.2.3 20030502 (Red Hat Linux 3.2.3-34)

Reply
Thread Tools

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

 
 
nymano
Guest
Posts: n/a
 
      08-26-2005
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.

 
Reply With Quote
 
 
 
 
Moritz Beller
Guest
Posts: n/a
 
      08-26-2005
On 26 Aug 2005 02:48:30 -0700
"nymano" <(E-Mail Removed)> wrote:

> class A
> {
> public:
> A() {}
> };
>
> 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ß
Moritz Beller
--
web http://www.4momo.de
mail momo dot beller at t-online dot de
gpgkey http://gpg.notlong.com
 
Reply With Quote
 
 
 
 
nymano
Guest
Posts: n/a
 
      08-26-2005
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.

 
Reply With Quote
 
nymano
Guest
Posts: n/a
 
      08-26-2005
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.

 
Reply With Quote
 
Sumit Rajan
Guest
Posts: n/a
 
      08-26-2005

"nymano" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.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 <(E-Mail Removed)>


 
Reply With Quote
 
Marc Mutz
Guest
Posts: n/a
 
      08-26-2005
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

 
Reply With Quote
 
nymano
Guest
Posts: n/a
 
      08-26-2005
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.

 
Reply With Quote
 
Chris Theis
Guest
Posts: n/a
 
      08-26-2005

"nymano" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.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


 
Reply With Quote
 
nymano
Guest
Posts: n/a
 
      08-26-2005
> 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.

 
Reply With Quote
 
Greger
Guest
Posts: n/a
 
      08-26-2005
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: Where to get stand alone Dot Net Framework version 1.1, version2.0, version 3.0, version 3.5, version 2.0 SP1, version 3.0 SP1 ? MowGreen [MVP] ASP .Net 5 02-09-2008 01:55 AM
Re: Where to get stand alone Dot Net Framework version 1.1, version 2.0, version 3.0, version 3.5, version 2.0 SP1, version 3.0 SP1 ? PA Bear [MS MVP] ASP .Net 0 02-05-2008 03:28 AM
Re: Where to get stand alone Dot Net Framework version 1.1, version 2.0, version 3.0, version 3.5, version 2.0 SP1, version 3.0 SP1 ? V Green ASP .Net 0 02-05-2008 02:45 AM
RED HAT LINUX vs LINUX SUSE SIP3261 VOIP 2 08-17-2004 10:32 PM
Red Hat Defines end of Red Hat Linux steve NZ Computing 14 11-06-2003 06:19 AM



Advertisments