Click here to Skip to main content
15,900,461 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hello, I get this error 'LETTERS' undeclared (first use in this function). LETTERS in declared after the includes like this:
C
enum LETTERS{
A = 0,B = 1,C = 2,D = 3,E = 4
}

then in the main void I get that annoying error:
C
int main(void) {
        int Word[] = {LETTERS.A,LETTERS.C,LETTERS.E};
    }
}

How can I get rid of this error?

Note:This happens in AVR Studio 5
Posted
Updated 19-May-11 1:27am
v2
Comments
#realJSOP 19-May-11 7:29am    
What is "AVR Studio 5"?
CPallini 19-May-11 7:42am    
Google is your friend... :-D
#realJSOP 19-May-11 15:47pm    
If I've never heard of it, it's not important enough for me to google it. :)
CPallini 19-May-11 16:01pm    
You're right, curiosity killed the cat: now I can't live without dealing somehow with microcontrollers.

LETTERS is a typename, not an instance of a struct. If you want to refer to elements of a structured type, you need to use the scope dereferencing operator '::' like this:
LETTERS::A
 
Share this answer
 
Comments
Niklas L 19-May-11 9:03am    
Sending a five in your general direction.
Albert Holguin 19-May-11 10:25am    
its not a typename in C... just pointing that out since the questions is tagged with both C/C++
Sergey Alexandrovich Kryukov 19-May-11 15:06pm    
This is the best syntax, but I hate to note it causes C4482 warning in MS which should be disabled: "nonstandard exception used". My 5 anyway.
--SA
Sergey Alexandrovich Kryukov 19-May-11 15:14pm    
Please see my answer, more complete.
--SA
Use
C++
int Word[] = {A,C,E};
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 19-May-11 15:15pm    
Correct, a 5, but...
Please see my answer, more complete.
--SA
Niklas L 20-May-11 2:52am    
Thanks.
Please see my reply to your more complete answer. :)
Hi,

-> First of all your Enum declaration should end with a semicolon.
-> Since enum type is used to set up named integer constants,you cant access its members with dot operator like you do with struct or class.
-> So you can use them like a #define-d constants by using its members directly.

example: int nVar = A;
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 19-May-11 15:15pm    
Correct, but there is a bit more to it. 5 for your answer, anyway.

Please see my answer, more complete.
--SA
You need to use either

int Word[] = {A, C, E};


or

C++
int Word[] = {LETTERS::A, LETTERS::C, LETTERS::E};


The last one is the best syntax, but I hate to note it causes C4482 warning in MS which should be disabled: "nonstandard exception used".

To suppress warning, use
C++
#pragma warning( disable : 4482)


This is not nice but should be done, sorry.

Last note: I hate to say, but as first syntax is acceptable (no type qualifier) it means that C++ enumeration members are too visible. That said, you should better choose longer and more descriptive names for those members, more suitable for the project-wide context. Again, hate to note that. This is one of the reasons I kind of dislike C++ — to many sloppy C decisions survived.

—SA
 
Share this answer
 
Comments
Niklas L 20-May-11 2:50am    
Maybe a somewhat constructed problem, since you should avoid placing things at file level in the global scope. Hence, you should always be able to qualify your enum values using the enclosing class or namespace.
Sergey Alexandrovich Kryukov 20-May-11 23:22pm    
That's a good idea, sure.
--SA
Stefan_Lang 23-May-11 4:30am    
I am not 100% sure, but C++0x (supported by VS 2010) changed the syntax for enums and it may well be that the second version (which I also suggested in my solution) is in fact wrong in C++0x. If that is the case then the warning might make sense for compilers supporting this standard, but older compilers should not issue it.
Sergey Alexandrovich Kryukov 23-May-11 19:00pm    
I recently tested this warning on VS 2008. I also have 2010, will see some time (do you know is there any difference with VS 2010? I doubt it?).
--SA
Stefan_Lang 24-May-11 3:51am    
I can't look it up now because my evaluation license expired, and I'm still waiting for the new workstation to install the full version, but when I did my tests I know that VS 2010 issued some errors (or warnings?) because of enum constants that did use the <enum_name>::<identifier> syntax.

The grammar for enums has changed in C++0x, and this is causing the issue, not VS 2010.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900