Compilation Error?

Discussion in 'C++' started by vikascoder@gmail.com, May 9, 2006.

  1. Guest

    Consider the following code snippet..

    #include<iostream>
    using namespace std;

    int func(void);

    int main() {
    cout<<"Result :"<<func()<<endl;
    }

    int func (void) {
    int result = 0;
    if (true) {
    result = 1;
    goto cleaup:

    }

    int i = 0; // i initialize a dummy here.. Error in XCode or Gcc !

    cleanup:
    return (result);
    }

    If i compile the above code with Visual Studio 6.0 compiler or
    CodeWarrior (Mac) it compiles without any error, but if i compile this
    code using gcc or XCode (Mac) it gives the following error..
    error: jump to label 'cleanup'
    error: from here
    error: crosses initialization of 'int i'

    The only way to solve this problem is to move the 'int i' definition
    below the 'int result' definition... is there any other way??
     
    , May 9, 2006
    #1
    1. Advertising

  2. wrote:
    > Consider the following code snippet..
    >
    > #include<iostream>
    > using namespace std;
    >
    > int func(void);
    >
    > int main() {
    > cout<<"Result :"<<func()<<endl;
    > }
    >
    > int func (void) {
    > int result = 0;
    > if (true) {
    > result = 1;
    > goto cleaup:
    >
    > }
    >
    > int i = 0; // i initialize a dummy here.. Error in XCode or Gcc !
    >
    > cleanup:
    > return (result);
    > }
    >
    > If i compile the above code with Visual Studio 6.0 compiler or
    > CodeWarrior (Mac) it compiles without any error, but if i compile this
    > code using gcc or XCode (Mac) it gives the following error..
    > error: jump to label 'cleanup'
    > error: from here
    > error: crosses initialization of 'int i'


    "It is possible to transfer into a block, but not in a way that
    bypasses declarations with initialization. A program that jumps from a
    point where a local variable with automatic storage duration is not in
    scope to a point where it is in scope is ill-formed unless the variable
    has POD type (3.9) and is declared without an initializer (8.5)."
    (6.7ยง3)

    > The only way to solve this problem is to move the 'int i' definition
    > below the 'int result' definition... is there any other way??


    Don't use goto?


    Jonathan
     
    Jonathan Mcdougall, May 9, 2006
    #2
    1. Advertising

  3. Ian Collins Guest

    wrote:
    > Consider the following code snippet..
    >
    > #include<iostream>
    > using namespace std;
    >
    > int func(void);
    >
    > int main() {
    > cout<<"Result :"<<func()<<endl;
    > }
    >
    > int func (void) {
    > int result = 0;
    > if (true) {
    > result = 1;
    > goto cleaup:
    >
    > }
    >
    > int i = 0; // i initialize a dummy here.. Error in XCode or Gcc !
    >
    > cleanup:
    > return (result);
    > }
    >
    > If i compile the above code with Visual Studio 6.0 compiler or
    > CodeWarrior (Mac) it compiles without any error, but if i compile this
    > code using gcc or XCode (Mac) it gives the following error..
    > error: jump to label 'cleanup'
    > error: from here
    > error: crosses initialization of 'int i'
    >

    I'm surprised this compiles with any compiler.

    > The only way to solve this problem is to move the 'int i' definition
    > below the 'int result' definition... is there any other way??
    >

    Sack whoever wrote this rubbish.

    --
    Ian Collins.
     
    Ian Collins, May 9, 2006
    #3
  4. Raj Guest

    it shudnt have worked with any of the compiler as far as myknowledge is
    concerned.. try the latest compiler version n u'll catch the error..
    the only workout is : goto is a bad practice.. stop using it..
     
    Raj, May 9, 2006
    #4
  5. Ian Collins Guest

    Raj wrote:
    > it shudnt have worked with any of the compiler as far as myknowledge is
    > concerned.. try the latest compiler version n u'll catch the error..
    > the only workout is : goto is a bad practice.. stop using it..
    >

    Are you answering me or the OP?

    Please quote context (see <http://cfaj.freeshell.org/google/>) in your
    replies and avoid txt speak.

    --
    Ian Collins.
     
    Ian Collins, May 9, 2006
    #5
    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. Vittal
    Replies:
    2
    Views:
    759
    Gordon Burditt
    Jul 8, 2003
  2. =?ISO-8859-1?Q?Martin_J=F8rgensen?=
    Replies:
    5
    Views:
    1,339
    =?ISO-8859-1?Q?Martin_J=F8rgensen?=
    May 6, 2006
  3. Gavin
    Replies:
    3
    Views:
    506
    Chris Uppal
    Mar 28, 2007
  4. C__chp
    Replies:
    4
    Views:
    547
    Puppet_Sock
    Feb 15, 2008
  5. Tassador

    const string error compilation - error C2679

    Tassador, Jul 19, 2009, in forum: C Programming
    Replies:
    0
    Views:
    643
    Tassador
    Jul 19, 2009
Loading...

Share This Page