parse error before `{'

justafisch's Avatar, Join Date: Mar 2011
Newbie Member
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 shabbir; 1Mar2011 at 16:06.. Reason: Code blocks
justafisch's Avatar, Join Date: Mar 2011
Newbie Member
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
xpi0t0s's Avatar, Join Date: Aug 2004
Mentor
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".
xpi0t0s's Avatar, Join Date: Aug 2004
Mentor
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++;
            }
        }
    }
}