enum fails with using declaration

M

Marshall Mills

As I understand it, loaded statement, a using declaration should be all I
need to see an enum from within a namespace. The below code works fine with
class, struct, and union. What gives? As the code says, if I employ the
using directive, I'm ok.

/* built with Visual C++ 6, SP 5 */
namespace Traffic
{
enum Light { red, yellow, green };
}

int main( void )
{
/*using namespace Traffic; /* using directive. OK */
using Traffic::Light; /* using declaration. error C2065: 'red' :
undeclared identifier */

Light broken = red;
return broken;
}
 
B

Ben Pfaff

Marshall Mills said:
As I understand it, loaded statement, a using declaration should be all I
need to see an enum from within a namespace.

C doesn't have using declarations. Please don't ask C++
questions in comp.lang.c.
 
R

Richard Heathfield

Marshall said:
As I understand it, loaded statement, a using declaration should be all I
need to see an enum from within a namespace. The below code works fine
with
class, struct, and union. What gives?

Please keep comp.lang.c out of this one. Thanks. :)
 
V

Victor Bazarov

Marshall Mills said:
As I understand it, loaded statement, a using declaration should be all I
need to see an enum from within a namespace. The below code works fine with
class, struct, and union. What gives? As the code says, if I employ the
using directive, I'm ok.

/* built with Visual C++ 6, SP 5 */
namespace Traffic
{
enum Light { red, yellow, green };
}

int main( void )
{
/*using namespace Traffic; /* using directive. OK */
using Traffic::Light; /* using declaration. error C2065: 'red' :
undeclared identifier */

Light broken = red;
return broken;
}

(a) As has been suggested, your post has nothing to do with C,
so comp.lang.c is trimmed from the response.

(b) You declared that you'll only be using 'Light' identifier.
'red' is a separate identifier, you need to declare using it
as well.

namespace N { enum A { a, b, c }; }

int main() {
using N::A;
using N::b; // comment this out to see the error message
A aa = b;
return 0;
}

Victor
 
M

Martin Ambuhl

Marshall said:
As I understand it, loaded statement, a using declaration should be all I
need to see an enum from within a namespace. The below code works fine with
class, struct, and union. What gives? As the code says, if I employ the
using directive, I'm ok.

You probably want to restrict your C++ questions to comp.lang.c++. When
you post to both that newsgroup and comp.lang.c, you really should have a
question which could be answered in either language. Those questions are
becoming rarer, with both C++ features not part of C and C features not
part of C++, as well as situations in which the answers are different --
either because the languages are different or because the usual idiom and
normal practice is different in the two languages.

In this case, there is no "using directive" or "using declaration" in C.
Namespace rules differ in the two languages, and named namespaces are not
part of C at all.

I have set followups to comp.lang.c++ only. Should you have a C question,
feel free to post that in comp.lang.c.

[C++ content follows, topical for one of the xposted newsgroups; please
ignore c.l.c]
Why not just use:

namespace Traffic
{
enum Light
{ red, yellow, green };
}

int main(void)
{

Traffic::Light broken = Traffic::red;
return broken;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,150
Messages
2,570,853
Members
47,394
Latest member
Olekdev

Latest Threads

Top