views:

107

answers:

2

I have the following chunk of a header file BKE_mesh.h:

/* Connectivity data */
typedef struct IndexNode {
    struct IndexNode *next, *prev;
    int index;
} IndexNode;
void create_vert_face_map(ListBase **map, IndexNode **mem, const struct MFace *mface,
          const int totvert, const int totface);
void create_vert_edge_map(ListBase **map, IndexNode **mem, const struct MEdge *medge,
          const int totvert, const int totedge);

Note that the header file was prepared for the possibility of being used in a C++ file, as it had:

#ifdef __cplusplus
extern "C" {
#endif

at the top of the file, and the needed finish at the bottom. But the class implementing it was written in C.

Next, whenever I try to #include the header file, I get an odd error. If the file has a .cpp extension, it compiles just fine, no complaints whatsoever. However, if I do:

#include "BKE_mesh.h"

inside of a file with a .c extension, I get the following errors:

expected ')' before '*' token

for the two last functions, in specific, the variable:

ListBase **map

in both classes. (Note that earlier in the header file, it declared, but not defined ListBase).

So, my question is: why is this valid C++ code, but not C code?

Thank you.

+6  A: 

In C++ you can refer to struct names directly but in C you need to prepend the keyword struct.

void create_vert_face_map(struct ListBase **map, ... );

You could get around this by adding a typedef. Then you wouldn't have to modify the function declaration.

typedef struct ListBase ListBase;
John Kugelman
/me slaps his head. Thanks. I think that's it. (ListBase was defined like this: struct ListBase;). It makes me wonder than how this works as a C file...Oh, also, shouldn't wrapping that in extern "C" { ... } cause it to have those same errors? Thank you.
Leif Andersen
Some C compilers used to autodetect this mistake back in the day. Some people used to compile there "C" libraries as if they were C++ with C linkage.
Joshua
@Leif Anderson: Wrapping the file in extern "C" { } does nothing except change how the linker works. You seem to be under the impression that C/C++ are the similar. Fortunately C++ is a completely different language and should be treated as such. Java has the same basic semantics as C but you don't consider the two languages to be interchangeable!
Martin York
@Marin. Ah, okay. I thought it actually used a C compiler, okay. Also, I was mainly intrigued that it didn't work as C code, because that's what it was originally, I wouldn't have posted this if it was the other way around (worked as C code, but not as C++ code).
Leif Andersen
A: 

Try running just the pre-processor for each case. Comparing the result may show different header files. If so, it may hint on the "C"-problem.

Yotam