parse error before `{'

Discussion in 'C' started by justafisch, Mar 1, 2011.

  1. justafisch

    justafisch New Member

    Joined:
    Mar 1, 2011
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    I keep getting this error when I run this code with my makefile. I researched common reasons for parse errors and I can't see any reason I should be getting this error

    Code:
    #include "lab2-api.h"
    typedef struct CircularBuffer {
    int beingUsed;
    int capacity;
    int count;
    int head;
    int tail;
    char buffer[12];
    } CB;
    int
    main (int argc, char *argv[]){
    int counter = 0;
    CB *cb;
    uint32 handle;
    char removed = '_';
    handle = dstrtol(argv[1], NULL, 10);
    cb = (CB *)shmat(handle);
     
    While(counter != 11){
    if(cb->beingUsed == 0){
    cb->beingUsed = 1;
    if(cb->count != 0){
    removed = cb->buffer[cb->head];
    if(cb->head == 11){
    cb->head = 0;
    }
    else{
    cb->head++;
    }
    cb->count--;
    }
    cb->beingUsed = 0;
    if(removed != '_'){
    Printf("Consumer removed : %c", removed);
    removed = '_';
    counter++;
    }
    }
    }
    }
    Any help would be greatly appreciated.
    Note: I have a consumer class that uses pretty much the exact same code and doesn't experience this error.
     
    Last edited by a moderator: Mar 1, 2011
  2. justafisch

    justafisch New Member

    Joined:
    Mar 1, 2011
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    Btw, this is the exact error I am recieving

    consumer.c: In function `main':
    consumer.c:24: parse error before `{'
    consumer.c: At top level:
    consumer.c:47: parse error before `}'
    make: *** [consumer.o] Error 1
     
  3. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    While and Printf are invalid, unless redefined in lab2-api.h. Should be while and printf, because C++ is case sensitive.

    There is no line 47 in the above code, are you sure you've posted the whole thing? The final closing brace is on line 40.

    Are you using an integrated development environment whereby you can press a button with the cursor on an error and have it highlight the line it doesn't like? Or are these just stdout messages from command line tools? If the former, perhaps you could get the debugger to identify lines "24" and "47".
     
  4. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    And here's the code properly formatted, thanks to Notepad++. Please use code blocks and post correcly formatted code - makes it a lot easier to read.
    Code:
    #include "lab2-api.h"
    typedef struct CircularBuffer {
        int beingUsed;
        int capacity;
        int count;
        int head;
        int tail;
        char buffer[12];
    } CB;
    int
    main (int argc, char *argv[]){
        int counter = 0;
        CB *cb;
        uint32 handle;
        char removed = '_';
        handle = dstrtol(argv[1], NULL, 10);
        cb = (CB *)shmat(handle);
    
        While(counter != 11){
            if(cb->beingUsed == 0){
                cb->beingUsed = 1;
                if(cb->count != 0){
                    removed = cb->buffer[cb->head];
                    if(cb->head == 11){
                        cb->head = 0;
                    }
                    else{
                        cb->head++;
                    }
                    cb->count--;
                }
                cb->beingUsed = 0;
                if(removed != '_'){
                    Printf("Consumer removed : %c", removed);
                    removed = '_';
                    counter++;
                }
            }
        }
    }
    
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice