Community
Participate
Working Groups
/* this file compiles with xlc */ typedef struct info { unsigned int flags; union { struct other_project { unsigned int : 0; }; struct current_project { int objectType; }; }; }info_t; int main(int argc, char *argv[]) { info_t test; test.flags = 0; /* completion works fine for next line */ test.objectType = 1; /* Eclipse shows: "Field 'objectType' could not be resolved" */ return 0; }
I do not know if your code is correct and anonymous structures in a union are allowed. Change your program to get named structures and your error will vanish: #include <iostream> using namespace std; typedef struct info { unsigned int flags; union { struct other_project { unsigned int : 0; } op; struct current_project { int objectType; } cp; }; }info_t; int main(int argc, char *argv[]) { info_t test; test.flags = 0; test.cp.objectType = 1; return 0; }
Sorry, I was to fast. :-) You need also a name for your union. typedef struct info { unsigned int flags; union { struct other_project { unsigned int : 0; } op; struct current_project { int objectType; } cp; } union_t; } info_t; int main(int argc, char *argv[]) { info_t test; test.flags = 0; test.union_t.cp.objectType = 1; return 0; }
In C++ an union is a class. According C++11 draft chapter 9.5 paragraph 5 "[...] Nested types and functions cannot be declared within an anonymous union.[...]". But this is what you did. You can change your code in this way to stay with an anonymous union: #include <iostream> using namespace std; struct other_project { unsigned int :0; }; struct current_project { int objectType; }; typedef struct info { unsigned int flags; union { struct other_project op; struct current_project cp; }; } info_t; int main( int argc, char *argv[] ) { info_t test; test.flags = 0; test.cp.objectType = 1; return 0; }
Of course you can define (not declare) structs inside your union typedef struct info { unsigned int flags; union { struct { unsigned int :0; } other_project; struct { int objectType; } current_project; }; } info_t; Now you can access the union members by test.current_project.objectType = 1;
I agree with AK that the code in comment #0 is not valid C or C++. Since Yves says it compiles with Xlc, I guess it's an Xlc extension. I don't know whether we accept patches implementing Xlc extensions into our C++ parser. If so, we should leave this bug open and mark it as being a request for implementing an Xlc extension. If not, we should close this bug as invalid.