a function-definition is not allowed here before ‘{’ token

I get this problem when I try to compile my code. After researching, there are 2 possible reasons to get this error. First, I forgot/added some braces. Second, it's because I might declare a function inside my main, but I didn't. Can you guys help me out?


My main method:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
void oxyAtom(int o);
int main(int argc, char** argv) {
    int oxygen = 0, hydrogen = 0,
        numOxy, numHydro,
        oxyIndex, hydroIndex, 
        mutex, oxyQueue, total,
        hydroBonded, hydroQueue;

    numOxy = stoi(argv[1]);
    numHydro = stoi(argv[2]);

    total = numOxy + numHydro;

    oxyIndex = numOxy;
    hydroIndex = numHydro;

    mutex = semget(SEMKEY_A, 1, 0700|IPC_CREAT);
    semctl(mutex, 0, SETVAL, 1); //mutex = Semaphore(1)

    oxyQueue = semget(SEMKEY_B, 1, 0700|IPC_CREAT);
    semctl(oxyQueue, 0, SETVAL, 0); //oxyQueue = Semaphore(0)

    hydroBonded = semget(SEMKEY_C, 2, 0700|IPC_CREAT);
    semctl(hydroBonded, 0, SETVAL, 0); //hydroBonded = Semaphore(0)
    semctl(hydroBonded, 1, SETVAL, 0);

    hydroQueue = semget(SEMKEY_D, 2, 0700|IPC_CREAT);
    semctl(hydroQueue, 0, SETVAL, 0); //hydroQueue = Semaphore(0)
    semctl(hydroQueue, 1, SETVAL, 0);

    //pthread_t t[total];
    thread t[total];

    //CREATING THREADS
    while(oxyIndex-1 >= 0 || hydroIndex-1 >= 0) {
        if(oxyIndex-1 >= 0) {
            t[oxyIndex-1] = thread(oxyAtom, oxyIndex);
            oxyIndex--;
        }
        if(hydroIndex-1 >= 0) {
            t[total-1] = thread(hydroAtom, hydroIndex);
            hydroIndex--;
        }
    }
    
    void oxyAtom(int o) {
    ...
    }
}



Here is the full error message:

In function ‘int main(int, char**)’:
error: a function-definition is not allowed here before ‘{’ token
     void oxyAtom(int o) {
Last edited on
Lines 46-48 are inside main, that's your oxyAtom function definition.

Move the closing bracket at line 49 to line 45.
Topic archived. No new replies allowed.