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

Discussion in 'C++' started by nymano, Aug 26, 2005.

  1. nymano

    nymano Guest

    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.
     
    nymano, Aug 26, 2005
    #1
    1. Advertising

  2. On 26 Aug 2005 02:48:30 -0700
    "nymano" <> 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
     
    Moritz Beller, Aug 26, 2005
    #2
    1. Advertising

  3. nymano

    nymano Guest

    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.
     
    nymano, Aug 26, 2005
    #3
  4. nymano

    nymano Guest

    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.
     
    nymano, Aug 26, 2005
    #4
  5. nymano

    Sumit Rajan Guest

    "nymano" <> wrote in message
    news:...
    > 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, Aug 26, 2005
    #5
  6. nymano

    Marc Mutz Guest

    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
     
    Marc Mutz, Aug 26, 2005
    #6
  7. nymano

    nymano Guest

    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.
     
    nymano, Aug 26, 2005
    #7
  8. nymano

    Chris Theis Guest

    "nymano" <> wrote in message
    news:...
    > 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
     
    Chris Theis, Aug 26, 2005
    #8
  9. nymano

    nymano Guest

    > 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.
     
    nymano, Aug 26, 2005
    #9
  10. nymano

    Greger Guest

    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
     
    Greger, Aug 26, 2005
    #10
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. AJ
    Replies:
    0
    Views:
    396
  2. Replies:
    2
    Views:
    3,305
    Malte
    Sep 1, 2005
  3. Avalon1178
    Replies:
    3
    Views:
    630
    Martin Honnen
    Sep 3, 2005
  4. Dave In Redmond

    Re: Update to Python 2.3 on Red Hat Linux 9

    Dave In Redmond, Aug 29, 2003, in forum: Python
    Replies:
    1
    Views:
    414
    Jay Bromley
    Aug 30, 2003
  5. Sharwan Joram
    Replies:
    4
    Views:
    388
    Sharwan Joram
    Nov 22, 2013
Loading...

Share This Page