Thread: Compile problems about ''local function definitions are illegal''

  1. #1
    Registered User
    Join Date
    May 2009
    Posts
    9

    Question Compile problems about ''local function definitions are illegal''

    Code:
    The details as follows:
    
    
    #include"stdio.h" 
    #include"malloc.h" 
    #define MaxSize 50 
    typedef int ElemType; 
    typedef struct 
    {ElemType elem[MaxSize]; 
    int length; 
    }SqList; 
    
    void CreateList(SqList *&L,ElemType a[],int n) 
    {int i; 
    L=(SqList *)malloc(sizeof(SqList)); 
    L->length=0; 
    for(i=0;i<n;i++) 
    L->elem[i]=a[i]; 
    L->length=n;} 
    
    void DestroyList(SqList *L) 
    {free(L);} 
    
    int ListEmpty(SqList *L) 
    {return(L->length==0);} 
    
    void BubbleSort(SqList *&L) 
    {int i,j,exchange;ElemType temp; 
    for(i=0;i<L->length-1;i++) 
    {exchange=0; 
    for(j=L->length-1;j>i;j--) 
    {if(L->elem[j]<L->elem[j-1]) 
    {temp=L->elem[j];L->elem[j]=L->elem[j-1];L->elem[j-1]=temp;exchange=1;}} 
    if(exchange==0) return;} 
    
    int DispList(SqList *L) 
    {int i; 
    if(ListEmpty(L)) return 0; 
    for(i=0;i<L->length;i++) 
    printf("%d\n",L->elem[i]); 
    return 1; 
    } 
    
    void ListInsert(SqList *&L,ElemType x) 
    {int i=0,j; 
    while(i<L->length&&L->elem[i]<x) i++; 
    if(L->length!=0&&i!=L->length) 
    {for(j=L->length-1;j>=i;j--) 
    L->elem[j+1]=L->elem[j];} 
    L->elem[i]=x;L->length++;} 
    
    void main() 
    {SqList *L; 
    ElemType a[8]={3,8,2,6,12,4,9,7}; 
    CreateList(L,a,8); 
    DispList(L); 
    BubbleSort(L); 
    ListInsert(L,5); 
    DispList(L); 
    DestroyList(L); 
    } 
    
    
    compile result with VC:--------------------Configuration: GE - Win32 Debug-------------------- 
    Compiling... 
    e.cpp 
    H:\Visual C++ 6.0\新建文件夹\MSDev98\MyProjects\GE\e.cpp(34) : error C2601: 'DispList' : local function definitions are illegal 
    H:\Visual C++ 6.0\新建文件夹\MSDev98\MyProjects\GE\e.cpp(42) : error C2601: 'ListInsert' : local function definitions are illegal 
    H:\Visual C++ 6.0\新建文件夹\MSDev98\MyProjects\GE\e.cpp(50) : error C2601: 'main' : local function definitions are illegal 
    H:\Visual C++ 6.0\新建文件夹\MSDev98\MyProjects\GE\e.cpp(59) : fatal error C1004: unexpected end of file found 
     Tread awry  when  execute cl.exe  
    
    GE.exe - 1 error(s), 0 warning(s) 
    
    I have looked repeatedly but failued to find the problem.  
    Does here any friend give me a hand?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    Indent your code properly and you will be able to see more clearly what the compiler is complaining about, i.e., an attempt to define a function within the definition of another function.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    In addition, you will find a missing closing brace (i.e. } ) is needed to ensure that the compiler doesn't believe you are defining functions within the BubbleSort() function.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    > #include"malloc.h"
    1. malloc is in stdlib.h, not malloc.h - it might work for you, but not for long
    2. All library includes should use the <> form, not the "" form

    > L=(SqList *)malloc(sizeof(SqList));
    3. Don't cast malloc in C, see the FAQ

    > MyProjects\GE\e.cpp
    4. If you really want to program in C, then name your source files as prog.c
    .cpp files are compiled as C++, which means you end up doing some awful hacks just to get your C code to compile (casting malloc being one of them)

    > CreateList(L,a,8);
    5. I take it that CreateList() modifies the list right.
    Well that change will NOT be reflected back in main.
    Also, L is uninitialised to being with (NULL is good)

    EDIT: Actually, it's OK, but that's only because your "C" program is actually a C++ program after all, and you're using reference parameters (a C++ only feature).
    Yet the rest of it is C, and you post on the C forum - CHOOSE YOUR LANGUAGE.

    > void main()
    6. int main - that's all there is to it.

    > {temp=L->elem[j];L->elem[j]=L->elem[j-1];L->elem[j-1]=temp;exchange=1;}}
    7. This kind of formatting will kill you every time. If you're going to do this (I strongly suggest you don't), at least make the braces match on the line.
    Putting }}}}}} is puke

    Compare
    Code:
    void BubbleSort(SqList *&L) 
    {
      int i,j,exchange;
      ElemType temp; 
      for(i=0;i<L->length-1;i++) 
      {
        exchange=0; 
        for(j=L->length-1;j>i;j--) 
        {
          if(L->elem[j]<L->elem[j-1]) 
          {
            temp=L->elem[j];
            L->elem[j]=L->elem[j-1];
            L->elem[j-1]=temp;
            exchange=1;
          }
        } 
        if(exchange==0) return;
      } 
    } /* YES, I added this one to your function */
    The compiler doesn't give a hoot about how many lines of code it takes and it makes NO difference to the efficiency of the generated code.
    A good indent style will mean you never have to play "hunt the missing brace" again.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. doubt in c parser coding
    By akshara.sinha in forum C Programming
    Replies: 4
    Last Post: 12-23-2007, 01:49 PM
  2. Problems with virtual function calls, please help
    By e66n06 in forum C++ Programming
    Replies: 12
    Last Post: 12-12-2007, 05:12 AM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  4. local function definitions are illegal
    By curlious in forum C++ Programming
    Replies: 2
    Last Post: 10-21-2003, 04:09 AM
  5. Local function definitions are illegal!?
    By Leeman_s in forum C++ Programming
    Replies: 2
    Last Post: 11-03-2001, 08:29 PM