error: 'cit' was not declared in this scope

John S. Fine johnsfine@verizon.net
Wed Dec 30 23:59:00 GMT 2009


Andrew Haley wrote:
> On 12/30/2009 03:38 AM, Gerry Sweeney wrote:
>
>   
>> Thats perfect and solved my problem, thank you very much. I now have
>> a better understanding of what the compiler is expecting.  The
>> compiler could certinaly do with a more explicit error message in
>> this scenario, perhaps something like:-
>>
>>     test.cpp:30: error: The item base_type::const_iterator is not known as a  type 
>> certainly would have helped
>>     
>
> Sure, but gcc doesn't tell you that because gcc doesn't know because
> the grammar is ambiguous.  This is really a bug in the language: its
> grammar isn't context-free.
>
>   
Having written many parsers myself (though not for anything approaching 
the difficulty of parsing C++) I know how hard it is to get a parser to 
generate decent error messages.  But in this case the error message is 
particularly lame:

               base_type::const_iterator cit = base_type::find(key);
generates the error message
test.cpp:30: error: expected ';' before 'cit'

The parser can't know what base_type::const_iterator is supposed to be 
(but knows that it might find out later).  I think it knows that it 
doesn't know what cit is and that it won't find out later in any way 
that could make this sequence retroactively correct.  So I understand 
why it sees an error at/before cit.

I also understand that ';' is one of many tokens that could follow a 
name such as base_type::const_iterator in a way that allows the parser 
to expect that name will be a member of base_type and continue parsing.  
But ';' isn't the only token that would be valid there.  So at that 
point in the parser code, the author must have known it was at least an 
unhelpful error message.

I would think the parser should "know" that base_type::const_iterator is 
at the heart of the problem.  And it knows that 
base_type::const_iterator hasn't been declared yet, and the author of 
the parser should know that the fact that base_type::const_iterator can 
be parsed before it is declared is an error prone feature of the language.

So in my opinion, it should be possible to generate an error more like:
Parse error after 'base_type::const_iterator', which is not yet declared 
because 'base_type' depends on templating.

I agree with what I think Andrew said that the parser should not try to 
guess that base_type::const_iterator was intended to be a type.  But I 
think it can reasonably guess that base_type::const_iterator is the 
proximate cause of the error.
Once the user understands that the compiler doesn't think 
base_type::const_iterator has been declared yet, the user should not 
have much trouble understanding the actual problem.

The code I port to GCC has far more of exactly this bug than most 
programmers will ever see.  So I'm already past the point where it 
matters at all what the error message says.  I will recognize this bug 
from any error message that identifies the approximate location in the 
source.  But I think there are plenty of people who will be porting code 
where a better error message would make a big difference.



More information about the Gcc-help mailing list