Getting the error: expected identifier or ‘(‘ before ‘{‘ token

c++

Need help with the error: expected identifier or '(' before '{' token. I have a main file and a sort heap file. This three files are all separate and I am trying to run this files on a terminal. The command line I use to run this files on terminal is:

gcc -std=c99 -DRAND -DHEAP -DTYPE=double -DPRINT main.c srtheap.c

This is my code:

/*
 *
 *  srt.h file
 *
 */

#ifndef SRT_H  
#define SRT_H  
#include < string.h >  
#define MAX_BUF 256

#define swap(qx,qy,sz) 

{                                             
  do                                                                 
   char buf[MAX_BUF];                                              
   char *q1 = qx;                                                  
   char *q2 = qy;                                                  
   for (size_t m, ms = sz; ms > 0; ms -= m, q1 += m, q2 += m) {    
       m = ms < sizeof(buf) ? ms : sizeof(buf);                    
       memcpy(buf, q1, m);                                         
       memcpy(q1, q2, m);                                          
       memcpy(q2, buf, m);                                         

  while (0)
}

void srtbubb(void *, size_t, size_t, int (*)(const void *, const void *));  
void srtheap(void *, size_t, size_t, int (*)(const void *, const void *));  
void srtinsr(void *, size_t, size_t, int (*)(const void *, const void *));  
void srtmerg(void *, size_t, size_t, int (*)(const void *, const void *));  

#endif /* SRT_H */

Best Solution

A #define of a multi-line macro requires that you end each (except the last) line with \. So you'd get:

#define swap(qx,qy,sz)                                              \
do {                                                                \
    char buf[MAX_BUF];                                              \
    char *q1 = qx;                                                  \
    char *q2 = qy;                                                  \
    for (size_t m, ms = sz; ms > 0; ms -= m, q1 += m, q2 += m) {    \
        m = ms < sizeof(buf) ? ms : sizeof(buf);                    \
        memcpy(buf, q1, m);                                         \
        memcpy(q1, q2, m);                                          \
        memcpy(q2, buf, m);                                         \
    }                                                               \
} while (0)

Be very careful to not have any white-space after the \ because this will cancel the \ and break the multi-line #define, which will result in hard to find compiler errors.

Related Question