When a forward declared class is not found the error message describes it incorrectly as a struct. See below for an example. I'd like to submit a patch to correctly display class. Let me know if such a patch would be considered. $ g++ -Wall -ggdb -O0 -o t test6.cpp test6.cpp: In function ‘int main()’: test6.cpp:8: error: invalid use of undefined type ‘struct foobar’ test6.cpp:4: error: forward declaration of ‘struct foobar’ File contains: ===================================== // g++ -Wall -ggdb -O0 -o t test6.cpp class foobar; int main() { foobar * o = new foobar; } ====================================
I don't think this matters anyways as class == struct and can be used interchangably for tags. if you summit a patch, see what this shows you: struct foobar; int main() { foobar * o = new foobar; } Or even this: struct foobar; class foobar; int main() { foobar * o = new foobar; } Since all three are the same code, so it is weird not to have struct == class here really.
Confirmed. I think a patch would be of interest. Maybe one could just tweak the error to read x.cc:5: error: invalid use of undefined type 'foobar' x.cc:1: error: forward declaration of struct or class 'foobar'
I just discovered that actually we *do* have machinery to do this, in general terms (see CLASSTYPE_DECLARED_CLASS, used by eg, class_key_or_enum_as_string). But cp_parser_elaborated_type_specifier doesn't use it, ie doesn't set CLASSTYPE_DECLARED_CLASS (at variance with cp_parser_class_head). Thus something like the following patchlet should do the trick. That said, Andrew has a point of course, for its second example we would say struct or class, whichever comes last in the redeclaration. All in all, seems an improvement to me, if the below works pretty well I'm going to submit it. ///////////// Index: parser.c =================================================================== --- parser.c (revision 179242) +++ parser.c (working copy) @@ -13423,8 +13423,15 @@ cp_parser_elaborated_type_specifier (cp_parser* pa } if (tag_type != enum_type) - cp_parser_check_class_key (tag_type, type); + { + /* Indicate whether this class was declared as a `class' or as a + `struct'. */ + if (TREE_CODE (type) == RECORD_TYPE) + CLASSTYPE_DECLARED_CLASS (type) = (tag_type == class_type); + cp_parser_check_class_key (tag_type, type); + } + /* A "<" cannot follow an elaborated type specifier. If that happens, the user was probably trying to form a template-id. */ cp_parser_check_for_invalid_template_id (parser, type, token->location);
Author: paolo Date: Tue Sep 27 21:52:19 2011 New Revision: 179293 URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=179293 Log: /cp 2011-09-27 Paolo Carlini <paolo.carlini@oracle.com> PR c++/31489 * parser.c (cp_parser_elaborated_type_specifier): For RECORD_TYPE, set CLASSTYPE_DECLARED_CLASS. /testsuite 2011-09-27 Paolo Carlini <paolo.carlini@oracle.com> PR c++/31489 * g++.dg/parse/error40.C: New. * g++.dg/warn/incomplete1.C: Adjust. Added: trunk/gcc/testsuite/g++.dg/parse/error40.C Modified: trunk/gcc/cp/ChangeLog trunk/gcc/cp/parser.c trunk/gcc/testsuite/ChangeLog trunk/gcc/testsuite/g++.dg/warn/incomplete1.C
Fixed for 4.7.0.