------- Comment #2 from jwakely dot gcc at gmail dot com 2009-08-10 11:34 ------- (In reply to comment #0) > To my mind statement > test.cpp: In constructor 'testclass::testclass()': > test.cpp:4: error: class 'testclass' does not have any field named 'number' > is redundant because statements > test.cpp:15: error: declaration of 'int testclass::number' > test.cpp:9: error: conflicts with previous declaration 'int > testclass::number() const' > that appear before already cover this case. We already have a member named > 'number' so we certainly can't have field with the same name. Diagnostic > claims > that a member 'number' already exist and field with that name cannot be > created > seems to be more reasonable here, but "test.cpp:15" and "test.cpp:9" already > said that.
The errors for lines 9 and 15 relate to the duplicate declaration of a member with the name 'number', whereas the error on line 4 refers to the invalid member initializer for 'number'. Those are separate errors. If you removed the declaration of 'int number;' (fixing the error on line 15) then there would still be an error on line 4, so I definitely don't consider that diagnostic to be redundant. > Besides I found it interesting that swapping public: and private: blocks > around > makes diagnostic even more unclear: Huh? The errors are the same except for swapping which declaration is treated as the duplicate and which as the previous declaration, and that's because you swappged the order. What did you expect? Why is it more unclear? (In reply to comment #1) > By the way using gcc instead of g++ doesn't changed things. Good - it shouldn't make any difference in this case. The difference with Comeau apears to be that name lookup finds the member variable 'number' whereas GCC finds the member function 'number' - maybe Comeau's name lookup finds the last declaration of that name and GCC's finds the member function - I'm not sure. Since the code is invalid neither compiler is right or wrong, and personally I don't have a strong preference. Both compilers point out that 'number' is declared twice, which is the main problem in the code that needs to be fixed, and both compilers print that as the first error. Note that if you put the declaration of 'int number;' first then Comeau has a very similar error to GCC's when the member function is first - it complains about the invalid member initializer and about the return statement in the member function. i.e. when the member function is declared later, Comeau's name lookup finds that one, and behaves similarly to GCC. I don't think there's much scope for improvement here. -- jwakely dot gcc at gmail dot com changed: What |Removed |Added ---------------------------------------------------------------------------- CC| |jwakely dot gcc at gmail dot | |com http://gcc.gnu.org/bugzilla/show_bug.cgi?id=41002