Vekta
Vekta

Reputation: 65

gcc error "expected ')' before '[' token"

I am receiving these errors while attempting to compile my program with GCC and i'm not sure whats causing them.

functions.h:21: error: expected ')' before '[' token
functions.h:22: error: expected ')' before '[' token
functions.h:23: error: expected ')' before '[' token
functions.h:25: error: expected ')' before '[' token
functions.h:26: error: expected ')' before '[' token
functions.h:27: error: expected ')' before '[' token

My program compiles fine in visual studio 2012.

Heres the header file that seems to be causing the errors.

struct subject
{
    char year[5];
    char session;
    char code[8];
    char credit[3];
    char mark[4];
};

struct data
{
    char name[30];
    char id[30];
    char cc[30]; 
    char course[80];
    struct subject subjects[30];
    int gpa;
};

void displayRecord(data [], int);
int nameSearch(data [], char [], int [], int);
void editRecord(data [], int, int);
char getChar(const char [], int);
int getData(data []);
void displayData(data []);
void deleteRecord(data [], int, int);

I'm invoking the compiler like this:

gcc -o test functions.cpp functions.h main.cpp

I'm stumped so any help would be appreciated!

Upvotes: 0

Views: 6883

Answers (2)

David Heffernan
David Heffernan

Reputation: 613481

The problem is that you are passing functions.h to the compiler. That is an include file and you should just let the two .cpp files include it. There's no need to pass it in your command line invocation of the compiler. Simply remove functions.h from your command line invocation of gcc.

Since this is C++, you should be using g++ rather than gcc to compile. Since you used gcc, the compiler treated functions.h as being C, and the code not valid C.

So, I think your compilation should be

g++ -o test functions.cpp main.cpp

Upvotes: 5

Mark B
Mark B

Reputation: 96301

My psychic debugging powers tell me that your visual studio is compiling the code as C++ while gcc is compiling it as C. Since you're missing the struct keyword before data in your function parameters the C compiler doesn't know what to do. Try running it through g++ instead of gcc (and possibly make sure your including source file's extension is .C or .cpp.

Upvotes: 5

Related Questions