0

I am trying to create an enum in C++ and use it in a class. But I am getting the following error:

TestClass.cpp:65:49: error: 'TestNameSpace::TestClass::Buttons' is not a class or namespace:Buttons::Right

I am using Visual Studio 2015. Any assistance would be appreciated.

Here is my code:

TestClass.h:

namespace TestNameSpace
{
    class TestClass
    {
    public:
        enum Buttons { Left, Right, Up, Down, Select, None };
        TestClass();
        ~TestClass();
    private:
        Buttons getButtonType(int buttonValue);
    };
}

TestClass.cpp:

#include "AirController.h"
namespace TestNameSpace
{
    Buttons TestClass::getButtonType(int buttonValue)
    {
        if (buttonValue < 50)   return Buttons::Right;
        if (buttonValue < 195)  return Buttons::Up;
        if (buttonValue < 380)  return Buttons::Down;
        if (buttonValue < 555)  return Buttons::Left;
        if (buttonValue < 790)  return Buttons::Select;
        return Buttons::None;
    }
}
4
  • 3
    You forgot to #include "TestClass.h" in TestClass.cpp. Also, you need TestClass::Buttons TestClass::getButtonType... because Buttons is inside TestClass. Commented Dec 1, 2015 at 11:11
  • 1
    Or, alternatively, you could use a trailing return type: auto TestClass::getButtonType(int buttonValue) -> Buttons {... } Commented Dec 1, 2015 at 11:13
  • @DavidSchwartz Thanks for the reply mate. I have updated the sample to include the header. I tried prefixing with the class name but it made no difference. Commented Dec 1, 2015 at 11:19
  • Can you paste a link to the code with those fixes? Commented Dec 1, 2015 at 11:27

3 Answers 3

3

I got your code to compile by simply adding TestClass:: qualification. That being said, you probably wanted enum class.

You need to #include "TestClass.h" in order to make it visible in that translation unit, when you split it across files.

Sign up to request clarification or add additional context in comments.

Comments

2

With classical enums, members are accessed without mentioning the name of the enum type. In other words, enum members live in the name space the enum lives in, not inside the enum.

That means, remove Buttons:: in front of the enum values:

TestClass::Buttons TestClass::getButtonType(int buttonValue)
{
    if (buttonValue < 50)   return Right;
    if (buttonValue < 195)  return Up;
    if (buttonValue < 380)  return Down;
    if (buttonValue < 555)  return Left;
    if (buttonValue < 790)  return Select;
    return None;
}

The name of the enum Buttons:: needs to be prefixed when you use the more modern way of declaring enums, that is "scoped enums" or "enum classes":

enum class Buttons { Left, Right, Up, Down, Select, None };

Then, you avoid name clashes with other stuff having the same name like your enum members.

Comments

-1

The enum definition may be placed outside of the class definition, if this is not a design requirement:

enum Buttons { Left, Right, Up, Down, Select, None };

then you access it like this:

Buttons::Left

9 Comments

And? I think OP knows this. Your edit is false; a standard enum puts all its members in the enclosing namespace, you seem to be talking about enum class.
This doesn't answer the question.
Placing the enum outside of the class definition works, so I think it does answer the question. the OP does not say the enum has to be within the class defintion.
No, it doesn't "work", it accomplishes a completely different thing. His code places the enum inside of the class, and that's enough to assume that he wants to have it inside of the class.
@BartekBanachewicz the OP asks for a solution, and does not pose any restrictions. In my opinion, it is you who is incorrect by making assumptions on what he might or might not want.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.