HI all I have this code taken from a C project. I am not able to compile it in any way under g++.
What can I do with this?
enum EnumInd开发者_运维百科exID{
ID_VALUE_A=2,
ID_VALUE_B=2
}
struct newtype {
enum MyEnumID eid;
const char *name;
} table[] = {
[ID_VALUE_A] = { MyEnumA, "ID_MSG_HeartbeatReq"},
[ID_VALUE_B] = { MyEnumB, "ID_MSG_HeartbeatReq"},
};
Are you sure that your compiler supports the Designated Initializer syntax?
g++ does not. From that hyperlink:
Standard C89 requires the elements of an initializer to appear in a fixed order, the same as the order of the elements in the array or structure being initialized.
In ISO C99 you can give the elements in any order, specifying the array indices or structure field names they apply to, and GNU C allows this as an extension in C89 mode as well. This extension is not implemented in GNU C++.
The following compiles fine as C99 (note: it's not valid as C89):
enum MyEnumID {
MyEnumA, MyEnumB
};
enum EnumIndexID{
ID_VALUE_A=2,
ID_VALUE_B=2
};
struct newtype {
enum MyEnumID eid;
const char *name;
} table[] = {
[ID_VALUE_A] = { MyEnumA, "ID_MSG_HeartbeatReq"},
[ID_VALUE_B] = { MyEnumB, "ID_MSG_HeartbeatReq"},
};
int main() { return 0; }
EDIT: others have noted that ID_VALUE_A
and ID_VALUE_B
are the same value, namely 2. That's probably a bug in your code. However, gcc accepts this.
Cheers & hth.,
You are missing a semicolon after the first enum definition.
Edit
Also, it turns out that this syntax is supported by GCC but isn't supported by G++ for some reason.
Apart from the missing semicolon after the enum
definition, the table[]
declaration is not syntactically correct won't compile on G++:
struct newtype {
enum MyEnumID eid;
const char *name;
} table[] = {
{ MyEnumA, "ID_MSG_HeartbeatReq"},
{ MyEnumB, "ID_MSG_HeartbeatReq"},
};
EDIT: Today i've learned about designated initialization.
well you need at least an entry point:
int main() {
.... lots of other code ...
}
精彩评论