1

I am trying to make a class and inside of the class I want to define an enum called Type. Can I use the enum I defined as a constructor for my class? If so, how do I access it outside of the class?

class Type {
public:
  enum TType { BLACK, WHITE, GRAY };
  Type(TType type, std::string value);
  ~Type();

...

This code doesn't give me any errors but when I try to create an instance of this class in another class it gives me an error because BLACK is not defined:

Type piece(BLACK, value);

Is there a special way to do this and still have TType as a class constructor for my Type class? This is my first time using enums so I don't know how they work exactly.

1 Answer 1

4

Yes you can:

Type piece(Type::BLACK, value);

enum TType is in scope of class Type, so you have to use scope resolution when accessing it outside Type's body and member functions.

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

3 Comments

I tried that as well but when I do that it gives me the error: no instance of constructor "Type::Type" matches the argument list argument types are: (Type::TType, string)
Actually it seems to be a problem with some other part of my code. I'm not sure what it up but in some areas of my code it gives me an error but not others. I will try and figure it out but thanks for the help.
@greyer no problem, BTW here is a clean example showing no error.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.