Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   parse error before `{' (http://www.go4expert.com/forums/parse-error-before-t25109/)

justafisch 1Mar2011 15:41

parse error before `{'
 
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.

justafisch 1Mar2011 15:51

Re: parse error before `{'
 
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 2Mar2011 13:37

Re: parse error before `{'
 
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 2Mar2011 13:38

Re: parse error before `{'
 
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++;
            }
        }
    }
}



All times are GMT +5.5. The time now is 23:49.