ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question

Need help with example using enum in C++ programming

asked 2016-03-28 22:31:43 -0600

Thang Nguyen gravatar image

I found this link talk about using enum. But I haven't been able to use it correctly. Could you please show me an example how to use it? Thank you very much.

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted

answered 2016-03-29 02:29:05 -0600

ahendrix gravatar image

updated 2016-03-29 14:51:11 -0600

Does this help?

namespace Choices
  enum Choice
typedef Choices::Choice Choice;

int foo() {
    Choice c = Choices::Choice1;

    switch(c) {
        case Choices::Choice1:
            // TODO: something
        case Choices::Choice2:
            // TODO: something
        case Choices::Choice3:
            // TODO: something
            // TODO: something

(tested and compiles with gcc 4.8.4)

edit flag offensive delete link more


Yes, it helps a lot. Thanks ahendrix.

But the correct should be Choices::Choice c = Choices::Choice1;

Thang Nguyen gravatar image Thang Nguyen  ( 2016-03-29 06:13:02 -0600 )edit

No; the typedef Choices::Choice Choice means that you should be able to use Choice as a type without requiring the Choices:: namespace.

ahendrix gravatar image ahendrix  ( 2016-03-29 14:49:17 -0600 )edit

I am sorry, you are correct. I define the typedef wrong so in declaring the variable I got an error which it didn't happen when I have the namespace. This makes me confused. Thank you very much :D

Thang Nguyen gravatar image Thang Nguyen  ( 2016-03-29 18:18:50 -0600 )edit

Question Tools

1 follower


Asked: 2016-03-28 22:31:43 -0600

Seen: 657 times

Last updated: Mar 29 '16