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

This is a discussion on Compile problems about ''local function definitions are illegal'' within the C Programming forums, part of the General Programming Boards category; 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; ...

  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
    21,662
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,266
    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%.

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,506
    > #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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

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, 12:49 PM
  2. Problems with virtual function calls, please help
    By e66n06 in forum C++ Programming
    Replies: 12
    Last Post: 12-12-2007, 04: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, 07:29 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21