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

N

nymano

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.
 
M

Moritz Beller

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
 
N

nymano

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.
 
N

nymano

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.
 
S

Sumit Rajan

nymano said:
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.
 
M

Marc Mutz

nymano wrote:
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
 
N

nymano

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.
 
C

Chris Theis

nymano said:
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
 
N

nymano

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.
 
G

Greger

nymano said:
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;
 

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top